Laravel问题试图获取非对象的属性'id'

时间:2018-10-23 15:39:32

标签: php laravel loops foreach laravel-blade

在这里,我尝试使用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>

error

之后,我就这样修改我的代码

<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>

输出 output

控制器

public function index()
    {
       $material = Material::all();
       $supplier = Supplier::all();

   return view('materialMgmt.material_suppliyer.create')->withMaterial($material)->withSupplier($supplier);
    }

如何避免出现此错误?

1 个答案:

答案 0 :(得分:0)

我按如下所示更改了代码,并且可以正常工作

<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>

法特梅赫先生,谢谢您的建议。