我有我的模型產品:多對多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