简体   繁体   中英

Inserting a JSON array from Laravel

I'm wondering how I can loop insert an array value to database through Laravel. A sample of a Json is here:

[{"rid":"252","recipient_id":"1","email_type":"Body","to_cc_bcc":"to","start_dte":"2016-05-18","end_dte":""},{"rid":"252","recipient_id":"5","email_type":"Body","to_cc_bcc":"to","start_dte":"2016-05-18","end_dte":""}]

And my controller for storing such is this:

public function store()
    {
        // validate
        // read more on validation at http://laravel.com/docs/validation
        $rules = array(
            'name'       => 'required',
        );
        $validator = Validator::make(Input::all(), $rules);

        // process the login
        if ($validator->fails()) {
            return Redirect::to('reports')
                ->withErrors($validator)
                ->withInput(Input::except('password'));
        } else {
            //Dump Recipient array
            $cleanRecipients = json_decode(Input::get('test'), true);
               foreach($cleanRecipients AS $value)
                    {
                            $report_recipient = new ReportRecipients;
                            $report_recipient->recipient_id       = $value['recipient_id'];
                            $report_recipient->rid       = $value['rid'];
                            $report_recipient->email_type = $value['email_type']; 
                            $report_recipient->to_cc_bcc = $value['to_cc_bcc'];
                            $report_recipient->start_dte = !empty($value['start_dte']) ? $value['start_dte'] : null;
                            $report_recipient->end_dte = !empty($value['end_dte']) ? $value['end_dte'] : null;

                    }
                        $report_recipient->save();

            // redirect
            Session::flash('message', 'Report was Successfully Saved!');
            return Redirect::to('reports');

What happens is that, it only stores the last set of values into the table and not all of them. I appreciate any help and thanks in advance.

Put your save() inside your loop. Also, you should do it in one transaction, to be atomic .

\DB::transaction(function() use($cleanRecipients) {
    foreach($cleanRecipients AS $value) {
        $report_recipient = new ReportRecipients;
        $report_recipient->recipient_id       = $value['recipient_id'];
        $report_recipient->rid       = $value['rid'];
        $report_recipient->email_type = $value['email_type']; 
        $report_recipient->to_cc_bcc = $value['to_cc_bcc'];
        $report_recipient->start_dte = !empty($value['start_dte']) ? $value['start_dte'] : null;
        $report_recipient->end_dte = !empty($value['end_dte']) ? $value['end_dte'] : null;
        $report_recipient->save();
});

You need to put $report_recipient->save(); inside your foreach loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM