Reputation: 3751
I am working on purchaseOrder
and purchaseOrderDetails
database table.
I am saving data to two tables from one form. Here is the appended row table for purchaseOrderrDetails
items. Insert data is working fine.
But, now I need to update the tables purchaseOrder
and purchaseOrderDetails
at a time.
I am trying to get all purchaseOrderDetails
data to my form which is using the appended row system. And, I am getting this error:
Property [id] does not exist on this collection instance
What should be the right code please someone helps me? Here are my controller method and edit view bellow-
Purchase_OrderController.php
//purchase order update form
public function update($id){
$suppliers = Supplier::all();
$categories = Category::all();
$purchaseorder_id = $id;
$purchaseorder= Purchase_Order::where('id', '=', $purchaseorder_id)->get();
$purchaseorder_details_id = Purchase_Order_Details::pluck('purchase_order_id');
$purchaseorder_details = Purchase_Order_Details::where('purchase_order_id', '=', $id)->get();
//$purchaseorder_details = Purchase_Order_Details::with('purchaseorder')->get();
//dd($purchaseorder_details);
return view('user/purchaseorder.edit')->with(['purchaseorder' => $purchaseorder, 'purchaseorder_details' => $purchaseorder_details, 'suppliers' => $suppliers, 'categories' => $categories]);
}
edit.blade.php
<tbody>
<?php $item_row = 0; ?>
@foreach($purchaseorder_details as $pur_detail)
<tr id="item-row-{{ $item_row }}">
<td class="text-center" style="vertical-align: middle;">
<button type="button" onclick="$(\'#item-row-' + item_row + '\').remove();" title="Delete" class="btn btn-xs btn-danger"><i class="fa fa-trash"></i></button>
</td>
<td>
<input value="{{ $pur_detail->code }}" class="form-control typeahead" required="required" placeholder="Item Code" name="item[{{ $item_row }}][code]" type="text" id="item-name-{{ $item_row }}">
</td>
<td>
<input value="{{ $pur_detail->item_name }}" class="form-control" required="required" name="item[{{ $item_row }}][item_name]" type="text" id="item-name-{{ $item_row }}">
</td>
<td>
<select class="form-control" required="required" name="item[{{ $item_row }}][category]" id="item-category-{{ $item_row }}">
<option selected="selected" value="">Select Category</option>
@foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
@endforeach
</select>
</td>
<td>
<input value="{{ $pur_detail->quantity }}" class="form-control text-right" required="required" name="item[{{ $item_row }}][quantity]" type="text" id="item-quantity-{{ $item_row }}">
</td>
<td>
<input value="{{ $pur_detail->uom }}" class="form-control text-right" required="required" name="item[{{ $item_row }}][uom]" type="text" id="item-uom-{{ $item_row }}">
</td>
</tr>
@endforeach
<?php $item_row++; ?>
<tr id="addItem">
<td class="text-center"><button type="button" onclick="addItem();" title="Add" class="btn btn-xs btn-primary" data-original-title="Add"><i class="fa fa-plus"></i></button></td>
<td class="text-right" colspan="5"></td>
</tr>
</tbody>
And first one is - purchaseOrder and second one is purchaseOrderDetails
Upvotes: 3
Views: 9348
Reputation: 50491
A Collection is a fancy object wrapper around an array, that is all. A Collection is NOT the things it contains, which is the mistake you are making.
$array = [
['id' => 1, 'name' => 'bob'],
['id' => 2, 'name' => 'tom'],
...
];
You wouldn't do this because you know the indexes you want are not on the array but the arrays inside of it:
$array['id'];
// but you would do this:
$array[0]['id'];
But with a Collection which is just an object around an array, you are doing that exact thing:
$collection->id;
Collections contain items, you want the 'id' from one of those items not the Collection itself as the Collection has no property named 'id'.
$collection->first()->id; // id from the first object
foreach ($collection as $item) {
$item->id;
}
etc ...
Upvotes: 6