Php 如何解决试图获取createProduct刀片文件中非对象的属性“id”时出现的此错误?

Php 如何解决试图获取createProduct刀片文件中非对象的属性“id”时出现的此错误?,php,laravel,Php,Laravel,我必须在创建产品表单选择列表中显示品牌名称,其中品牌名称来自品牌模型,但当我查看创建产品表单时,出现此错误 正在尝试获取非对象视图的属性“id”:C:\xampp\htdocs\MobileShop\resources\views\admin\product\createProduct.blade.php 我的品牌模式是: class brand extends Model { protected $connection = 'mysql'; protected $primaryK

我必须在创建产品表单选择列表中显示品牌名称,其中品牌名称来自品牌模型,但当我查看创建产品表单时,出现此错误

正在尝试获取非对象视图的属性“id”:C:\xampp\htdocs\MobileShop\resources\views\admin\product\createProduct.blade.php

我的品牌模式是:

class brand extends Model
 {

  protected $connection = 'mysql';

  protected $primaryKey = 'id';

  protected $fillable = ['brandName'];

  public $timestamps = false;

  public function product(){

 return $this->hasMany('App\Product');

     }

   protected $table = 'brands';
}

我的产品型号是

class Product extends Model

  {
  protected $connection = 'mysql';

  protected $table = 'products';

  protected $primaryKey = 'id';

  public $timestamps = false;

  protected $fillable = 
       ['productName','brand_id','description','price','image'];


   public function brand(){

        $this->belongsTo('App/brand');
   }
 }
ProductController创建方法

enter  public function create()
  {
    $product = brand::pluck('brandName','id');

    return view('admin.product.createProduct',compact('product'));
  }
我的createProduct刀片视图文件是

   <form class="form-horizontal" method="POST" action="{{url('/product')}}" enctype="multipart/form-data" >
                  {{csrf_field()}}

                    <div class="form-group">
                        <label for="name" class="col-md-4 control-label">Product Name :</label>

                        <div class="col-md-6">
                            <input id="name" type="text" class="form-control" name="name" required autofocus>

                        </div>
                    </div>

                   <div class="form-group">
                       <label for="name" class="col-md-4 control-label">Brand:</label>

                       <div class="col-md-6">

                            <select name="cars" class="form-control" >
                             @foreach($product as $item)
                               <option value="{{$item->id}}">  #-->this line error occure#
                                   {{$item->brandName}}
                               </option>
                             @endforeach
                           </select>

                       </div>
                   </div>

                   <div class="form-group">
                       <label for="description" class="col-md-4 control-label">Description :</label>

                       <div class="col-md-6">
                           <textarea name="description" id="description" class="form-control" ></textarea>

                       </div>
                   </div>

                   <div class="form-group">
                       <label for="price" class="col-md-4 control-label">Price :</label>

                       <div class="col-md-6">
                           <input id="price" type="text" class="form-control" name="price" required autofocus>

                       </div>
                   </div>

将第二个参数传递给Pulk意味着您有一个key=>value数组brandName将是value,id将是key

所以$product=brand::去掉'brandName','id';将为您提供如下数组:

['id1' => 'brandName1', 'id2' => 'brandName2']
因此,在模板中,您应该以以下方式进行迭代:

@foreach($product as $id => $name) 
{{ $id }} - {{ $name }}
@endforeach

$item在哪个表中?尝试$item['id']而不是$item->id更改此$this->belongsTo'App/brand';到$this->belongsTo'App\brand';您在函数中使用了错误的“/”。谢谢您的帮助
@foreach($product as $id => $name) 
{{ $id }} - {{ $name }}
@endforeach