简体   繁体   中英

How to bulk insert in laravel 5.4 using eloquent

i have a html form, in my form i want to bulk insert into my database using laravel eloquent.

here is my save method.

public function storecheque(Request $request)
{
    $directcheque=new Directcheque();
    $directcheque->client_id=$request->input(['client_id']);
     $directcheque->bank_id=$request->input(['b_name']);
     $directcheque->cheq_amt=$request->input(['cheqamt']);
     $directcheque->cheq_no=$request->input(['cheq']);
     $directcheque->collc_date=$request->input(['cdate']);

        $directcheque->save();
        redirect(route('directcheqentry'));


}

Here is my form view.

  <div class="row">
            <div class="col-md-2">
              <select name="b_name[]" class="form-control b_name" required="">
              <option value="">-Select Bank-</option>
              @foreach($banks as $bank)
              <option value="{{$bank->id}}">{{$bank->bank_name}}</option>
             @endforeach
            </select>
                </div>

       <div class="col-md-3">
        <input type="text" name="cheqamt[]" class="form-control cheq" value="0" required="">
      </div>
            <div class="col-md-2">
              <input type="text" name="cheq[]" class="form-control cheq" value="0" required="">
            </div>
            <div class="col-md-2">
              <input type="date" name="cdate[]" class="form-control cdate" required="">
            </div>

What is your error?

I think you can use request without input() like this:

 .....
 $directcheque->client_id=$request->client_id;
 ....

Hope it helps :)

This is an example and not the SOLUTION, so please bear with me.

It is possible to post the all inputs in an Array so it is received as PHP Array as well, like this:

 <div class="row"> <form action="/thecontrollername" method="post"> <div class="col-md-2"> <select name="cheq[bank_name]" class="form-control b_name" required=""> <option value="">-Select Bank-</option> @foreach($banks as $bank) <option value="{{$bank->id}}">{{$bank->bank_name}}</option> @endforeach </select> </div> <div class="col-md-3"> <input type="text" name="cheq[amt]" class="form-control cheq" value="0" required=""> </div> <div class="col-md-2"> <input type="text" name="cheq[amount]" class="form-control cheq" value="0" required=""> </div> <div class="col-md-2"> <input type="date" name="cheq[date]" class="form-control cdate" required=""> </div> <div class="col-md-2"> <input type="submit" name="submit" value="Save"> </div> </form> </div> 

and then in your controller you can receive the Array "cheq"

now in the controller store function or where ever it is you can call this:

$cheq = $request->input('cheq');

Now you can initialize your $directcheque like this:

$directcheque=new Directcheque($cheq);

and save it or do what you want with it.

$directcheque->save();

Just put the right field names into the form and it will do.

if you receive an error saying:

General error: 1364 Field 'some_field' doesn't have a default value

make sure that:

protected $fillable = [ 'some_field']; is declared in the model class and $fillable must have all the keys (field names) which you wish to populate via the initialization by the above method.

Cheers

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