1

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.

2 Answers 2

2

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();
});
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the tip.. I'll create a function in the DB afterwards.
1

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

1 Comment

Silly me..of course..more coffee..I'll check this answer after 5 more minutes since it only took a minute for the post to before getting answered. :D Thanks a heap

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.