mercredi 27 janvier 2016

Laravel Save Relations with one line code

I have an idea but I'm not sure if it is possible with laravel.

I'm using AngularJS on my frontend, and I'm posting the data to backend. Data structure is the same of backend.

Please check this data format to understand what I am doing : http://ift.tt/1Sjz3Tp

Models are related between the others very well (like hasMany, belongsTo et...)

How do I save data currently?

public function store(Request $request)
{

    $data=$request->all();
    $invoice=Invoice::create($data);

    foreach ($data['invoice_products'] as $invoiceProduct) {
        $invoiceProduct['tax_1_id']=$invoiceProduct['tax_1']['id'];
        $invoiceProduct['tax_2_id']=$invoiceProduct['tax_2']['id'];
        $invoice->invoiceProduct->create($invoiceProduct)   
    }
    return $invoice;
}

So, my question is are there a way to save all data with a sinle line code like this;

public function store(Request $request)
{

    return $invoice=Invoice::saveWithRelations($request->all());

}

I want to do that, because all relations are defined well. It would be great this kind of saving. Are there any way to do this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire