jeudi 15 octobre 2020

How to insert multiple record without loop in laravel

I have I need to insert multiple record in database . Currently I am inserting with loop which is causing timeout when record is big. Is there any way that we dont use loop?

$consignments =   Consignment::select('id')->where('customer_id',$invoice->customer_id)->doesntHave('invoice_charges')->get();
       foreach($consignments as $consignment){
         InvoiceCharge::create(['invoice_id'=>$invoice->id,'object_id'=>$consignment->id,'model'=>'Consignment']);
       }

consignment has hasOne relation in model

public function invoice_charges()
    {
        return $this->hasOne('App\Models\Admin\InvoiceCharge', 'object_id')->where('model', 'Consignment');
    }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire