我有我的模型产品:多对多Laravel关系
class Product extends Model
{
protected $table = 'products';
protected $primaryKey = 'id_product';
protected $fillable = [
'id_category', 'title', 'credit', 'created_at', 'updated_at',
];
public function categories()
{
return $this->belongsToMany('App\Category', 'products_categories', 'id_product', 'id_category');
}
}
我有我的分类模型:
class Category extends Model
{
protected $table = 'categories';
protected $primaryKey = 'id_category';
protected $fillable = [
'category', 'created_at', 'updated_at',
];
public function products()
{
return $this->belongsToMany('App\Product', 'products_categories', 'id_product', 'id_category');
}
}
我有我的表products_categories
我想列出belog的产品所以我这样做:
$products = Product::with('categories')->get();
foreach ($products as $product) {
$products->title;
}
但它不工作,我想知道...我如何列出?
我试过所有..它说在这个集合实例中不存在属性[标题]。
谢谢
执行'dd($ product)',那里有什么? – apokryfos