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');
    }


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/34XUyW1
via IFTTT

Aucun commentaire:

Enregistrer un commentaire