0

I tried to replicate the rows but without results, I get orders replicate without the corresponding items.

ORDERS pk(id) auto increment
id       total       status       date
-----+-------------+-----------+-------------
118      899.58        2          2015-03-03 00:18:58
119      38.55         2          2015-03-03 00:18:58


ITEMS pk(order_id,product_id) corresponding items childs:
order_id    product_id    quantity
----------+-------------+----------
118         1115          82 
119         8965          12      /// ro replicate


$idnew = Order::find($idorder)->replicate()->save(); // create new OK
$idnewId = $idnew->id;/////obtain id of last insert  
$itemstemp = DB::table('item')->where('order_id', '=' ,$idnewId)->get();

           foreach($itemstemp as $itemte){
            DB::table('item')->insert(array(
                'order_id'   => $itemte->order_id, 
                'product_id' => $itemte->product_id,
                'quantity'   => $itemte->quantity
                )); 
            }

Response ErrorExceptionTrying to get property of non-object line $idnewId = $idnew->id;

Desired Output:

ORDERS pk(id)
id       total       status       date
-----+-------------+-----------+-------------
118      899.58        2          2015-03-03 00:18:58
119      38.55         2          2015-03-03 00:18:58
120      38.55         2          2015-03-03 00:18:58

ITEMS pk(order_id,product_id) corresponding items childs:
order_id    product_id    quantity
----------+-------------+----------
118         1115          82 
119         8965          12  
119         2255          22
120         8965          12  
120         2255          22  

Any ideas please

1 Answers1

0

save() just returns a boolean if saving worked or not. What you should do instead is this:

$new = Order::find($idorder)->replicate();
$new->save();
$itemstemp = DB::table('item')->where('order_id', '=' ,$new->id)->get();
// and so on...

Also I think your logic is a bit wrong. Something like that would make more sense to me:

$new = Order::find($idorder)->replicate();
$new->save();
$items = DB::table('item')->where('order_id', $idorder)->get();
$newItems = [];
foreach($items as $item){
    $newItems[] = [
        'order_id' => $new->id,
        'product_id' => $item->product_id,
        'quantity'   => $item->quantity
    ];
}
DB::table('item')->insert($newItems);
lukasgeiter
  • 147,337
  • 26
  • 332
  • 270
  • Thanks Luka, i try that and execute querie without error, but no insert child items rows – Userlaravel Mar 03 '15 at 13:54
  • Are you sure? Please double check your code with my answer. Because `replicate()` should definitely return an object (a model instance to be precise) – lukasgeiter Mar 03 '15 at 13:55
  • yes, sorry. I changed a variable mistakenly misspelled. Now execute querie without error, but no insert child items rows – Userlaravel Mar 03 '15 at 14:00
  • Because you're logic is wrong. You are looking up items by the new order id instead of the old one. Take a look at my edited answer. – lukasgeiter Mar 03 '15 at 14:03