Merhaba arkadarşlar,
Laravel ile yapmaya çalıştığım bir tablo var. Bu tabloda birbiri ile ilişkili varileri tek bir sorguda çeçemedim ayrıca yeni veri ekleme işleminde de nasıl yapılması gerektiğini anlatabilir misiniz?
Migration
Schema::create('products', function(Blueprint $table) {
$table->engine = 'InnoDB';
$table->bigIncrements('id');
$table->string('name');
});
Schema::create('attributes', function(Blueprint $table) {
$table->engine = 'InnoDB';
$table->bigIncrements('id');
$table->string('dimension');
$table->string('weight');
$table->string('page_number');
$table->string('desc');
});
Schema::create('attribute_product', function (Blueprint $table){
$table->engine = 'InnoDB';
$table->bigIncrements('id');
$table->unsignedBigInteger('product_id');
$table->unsignedBigInteger('attribute_id');
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->foreign('attribute_id')->references('id')->on('attributes')->onDelete('cascade');
});
Schema::create('categories', function(Blueprint $table) {
$table->engine = 'InnoDB';
$table->bigIncrements('id');
$table->string('name');
$table->string('desc');
});
Schema::create('attribute_category', function(Blueprint $table) {
$table->engine = 'InnoDB';
$table->bigIncrements('id');
$table->unsignedBigInteger('attribute_id');
$table->unsignedBigInteger('category_id');
$table->foreign('attribute_id')->references('id')->on('attributes')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
});`
Models
-Product
public function attributes(){
return $this->belongsToMany(Attribute::class);
}
-Attribute
public function products(){
return $this->belongsToMany(Product::class);
}
public function categories(){
return $this->belongsToMany(Category::class);
}
-Category
public function attributes(){
return $this->belongsToMany(Attribute::class);
}
Denediğim kod
$result = Product::with('attributes')->get();
Bu kodda category tablosuna erişemiyorum.Acaba ilişkilendirmede mi bir yanlışlık var yoksa tablo oluşturmada mı?
Şimdiden Teşekkürler