Vikram Anand Bhushan
Vikram Anand Bhushan

Reputation: 4886

How to insert multiple rows in database using Eloquent in Laravel5

My controller looks something like this

    <?php

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Auth;
    use App\Http\Requests;
    use App\Http\Controllers\Controller;
    use App\Http\Middleware\Role;
    use Illuminate\Support\Facades\Input;
    use App\Http\Requests\PaymentRequest;
    use App\User;
    use App\Invoice;
    use App\comments;
    use Session;
    use Validator;


    class CollectionController extends Controller
    {
        /**
         * Display a listing of the resource.
         *
         * @return Response
         */

      public function __construct(){

        $this->middleware('role:collector'); // replace 'collector' with whatever role you need.
    }


  public function postPayment(PaymentRequest $request){

          $insertPayment=Input::get();

          $payment= new paymentrecieved();
          $comment= new comments();

          $payment->invoice_id=$insertPayment['invoiceid'];
          $payment->recieved_amount=$insertPayment['recieved_amount'];
          $payment->refno=$insertPayment['ref_no'];
          $payment->date=$insertPayment['date'];

         if($insertPayment['adjustmentmode']=='Option')
          $payment->adjust_mode='NONE';
          else
            $payment->adjust_mode=$insertPayment['adjustmentmode'];
            $payment->save();

          $request->session()->flash('alert-success', 'Payment Has Been inserted Successfully');
          return redirect('collection/payment/1');

          //dd($insertPayment); 
 }




        }

So in my form the Payment related details are in an array I want to know how I can create multiple row ? and save it all together once .

Thanks & Regards

Upvotes: 2

Views: 3317

Answers (1)

Jobin
Jobin

Reputation: 8282

Try this,

$result = Banner::create([
            'name' => $name,
            'status' => $status,
            'description' => $description,
            'category_id' => $category_id,
            'ordering' => $ordering,
            'link' => $link,
        ]);
$result = Banner::create($data);

You can pass a multidimensional array in that case you have to use insert

for(..){
$data[] = [
                'name' => $name,
                'status' => $status,
                'description' => $description,
                'category_id' => $category_id,
                'ordering' => $ordering,
                'link' => $link,
            ];
}

 $result = Banner::insert($data)

note: this will not update/create timestamps, so you have to pass that too with insert array.

Hope it helps..

Upvotes: 5

Related Questions