Laravel 5.5 中的此集合实例上不存在属性 [id]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47647877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:02:52  来源:igfitidea点击:

Property [id] does not exist on this collection instance in laravel 5.5

phpmysqllaravellaravel-5

提问by Rashed Hasan

I am working on purchaseOrderand purchaseOrderDetailsdatabase table.

我正在处理purchaseOrderpurchaseOrderDetails数据库表。

I am saving data to two tables from one form. Here is the appended row table for purchaseOrderrDetailsitems. Insert data is working fine.

我正在将数据从一种形式保存到两个表中。这是项目的附加行表purchaseOrderrDetails。插入数据工作正常。

But, now I need to update the tables purchaseOrderand purchaseOrderDetailsat a time.

但是,现在我需要更新表purchaseOrder,并purchaseOrderDetails在同一时间。

I am trying to get all purchaseOrderDetailsdata to my form which is using the appended row system. And, I am getting this error:

我正在尝试将所有purchaseOrderDetails数据添加到使用附加行系统的表单中。而且,我收到此错误:

Property [id] does not exist on this collection instance

此集合实例上不存在属性 [id]

What should be the right code please someone helps me? Here are my controller method and edit view bellow-

什么应该是正确的代码请有人帮助我?这是我的控制器方法和下面的编辑视图-

Purchase_OrderController.php

采购订单控制器.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

编辑刀片.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 purchaseOrder

第一个是 - purchaseOrder,第二个是 purchaseOrderDetails 采购订单

purchaseOrderDetails

采购订单详情

回答by lagbox

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.

Collection 是一个围绕数组的奇特对象包装器,仅此而已。集合不是它包含的东西,这是你犯的错误。

$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,您正在做的就是这样:

$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'.

集合包含项目,您需要来自这些项目之一的“id”而不是集合本身,因为集合没有名为“id”的属性。

$collection->first()->id;   // id from the first object

foreach ($collection as $item) {
    $item->id;
}

etc ...