Laravel问题试图获取非对象的属性“id”
Posted
技术标签:
【中文标题】Laravel问题试图获取非对象的属性“id”【英文标题】:Laravel issue Trying to get property 'id' of non-object 【发布时间】:2019-03-27 22:08:44 【问题描述】:我在这里尝试使用 foreach 循环来循环供应商名称,但出现以下错误
<tbody style="font-size:small">
@foreach($material as $material)
<tr class="odd" role="row">
!! Form::open(['route'=>'materialsupplier.store','class'=>'form-horizontal p-t-20']) !!
<td> $material->name <input type="hidden" value=" $material->id " name="mat_id"></td>
<td></td>
<td>
<div class="form-group $errors->has('sup_id') ? ' has-error' : '' ">
<div >
<select class="form-control js-cities" name="sup_id">
<option value="" selected disabled>Select Supplier</option>
@foreach ($supplier as $supplier)
<option value=" $supplier->id">$supplier->company_name</option>
@endforeach
</select>
</div>
</div>
</td>
<td>
Form::submit('Save',['class'=>'far fa-trash-alt btn btn-info btn-sm btn-rounded m-b-1 m-l-5'])
</td>
!! Form::close() !!
</tr>
@endforeach
</tbody>
之后我像这样修改了我的代码
<select class="form-control js-cities" name="sup_id">
<option value="" selected disabled>Select Supplier</option>
@foreach ($supplier as $supplier)
<option value=" $supplier">$supplier</option>
@endforeach
</select>
输出
控制器
public function index()
$material = Material::all();
$supplier = Supplier::all();
return view('materialMgmt.material_suppliyer.create')->withMaterial($material)->withSupplier($supplier);
如何避免出现此错误?
【问题讨论】:
首先,数组表达式和值同名!请更改它:@foreach($material as $material)
和 @foreach ($supplier as $supplier)
谢谢您,先生,您的建议对我帮助很大
【参考方案1】:
我将代码更改如下,它的工作原理
<select class="form-control js-cities" name="sup_id">
<option value="" selected disabled>Select Supplier</option>
@foreach ($supplier as $sup)
<option value=" $sup->id">$sup->company_name</option>
@endforeach
</select>
FatemehNB 先生,感谢您的建议。
【讨论】:
以上是关于Laravel问题试图获取非对象的属性“id”的主要内容,如果未能解决你的问题,请参考以下文章