laravel - Create collection from input array -


lets have form invoice. has line items $product[$key], $quantity[$key]. when form submitted input looks like

{   customer_id : "214"   product_id: [ "1","5", "6" ],   quantity: ["34", "1", "54"] } 

i have model details table. have been doing iterating on , creating details object saving this

foreach($product $key=>$p) {   if($p)   {     $t = new details;     $t->product = $p;     $t->quantity = $quantity[$key];     $t->save();   } } 

i'm guessing there way more efficient this. creating collection of details straight input have no idea how accomplish that

you can instantiate models through mass assignment.

$details = new details(['product_id'=>'1','quantity'=>'34']); 

you can specify columns of table not want mass assigned using $guarded variable in model.

check out mass assignment in laravel's docs: http://laravel.com/docs/eloquent#mass-assignment

for particular issue looks need build input array out of elements of other arrays.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -