ich mein Modell Produkte haben:ManyToMany Laravel Beziehung
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');
}
}
ich meine Kategorie Modell haben:
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');
}
}
ich meinen Tisch products_categories haben
Ich will die Produkte aufzulisten, die Belog zu eine Kategorie also mache ich das:
$products = Product::with('categories')->get();
foreach ($products as $product) {
$products->title;
}
Aber es funktioniert nicht, ich möchte wissen ... wie kann ich auflisten?
Ich habe alles versucht .. und es heißt Eigenschaft [Titel] existiert nicht auf dieser Sammlung Instanz.
Dank
Tun Sie ein 'dd ($ product)', was ist da drin? – apokryfos