2

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

1 Answer 1

5

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..

Sign up to request clarification or add additional context in comments.

4 Comments

I am trying to understand your idea :P
Its throwing MassAssignmentException in Model.php line 424: exception do I need to assign those variable as fillabel in the Request class
Yes, if you use the create method and want to be able to assign those fields, they need to be fillable. Also, in order to insert a multidimensional array, you would need to use the insert method, not the create method.
@ThomasKim thanks for the point, I missed that anyway corrected :)

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.