I'm working to build up a Laravel API and I'm having dome troubles with the models relationships and not sure if I'm doing this well.
I have the following models on my project:
- Brand
- Category
- Kind
- Product
- Size
- ProductImages
This models represents the products available on a ecommerce and I'm trying to relate it the following way:
1 product has 1 brand + 1 category + 1 kind + n ProductImages (one product n images for every image belongsTo one product only) + n sizes (I would like to use the same size table row for multiple registers).
I want this related on the database so I from 1 product I can get all the related information such as brand or category but also I need to do it on reverse from one brand get all the products for example.
So I've started doing the following model functions:
Brand:
public function product(){
return $this->hasMany(Product::class);
}
Category:
public function product(){
return $this->hasMany(Product::class);
}
Kind:
public function product(){
return $this->hasMany(Product::class);
}
Product:
public function category(){
return $this->belongsTo(Category::class);
}
public function brand(){
return $this->belongsTo(Brand::class);
}
public function size(){
$this->belongsTo(Size::class);
}
public function kind(){
return $this->belongsTo(Kind::class);
}
public function productImages(){
return $this->hasMany(ProductImages::class);
}
Size:
public function product(){
$this->belongsTo(Product::class);
}
ProductImages:
public function product(){
$this->belongsTo(Product::class);
}
If I do query categories on Tinker I get the list of categories bu I f I try to get all the products belonging on a same category I get nothing but not sure if its right to relate all the models like I'm doing.
When I check product's brand on tinker this is what I get:
>>> $product = App\Models\Product::First();
=> App\Models\Product {#4307
id: "1",
descrption: "Voluptatibus et eius enim aut ipsa earum quis veritatis.",
productImages_id: null,
size_id: null,
brand_id: "1",
category_id: null,
kind_id: "1",
created_at: "2021-03-01 11:19:18",
updated_at: "2021-03-02 10:26:14",
}
>>> $product->brand();
=> Illuminate\Database\Eloquent\Relations\BelongsTo {#4302}
MIgrations:
Products table:
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('descrption');
$table->integer('productImages_id')->nullable();
$table->integer('size_id')->nullable();
$table->integer('brand_id')->nullable();
$table->integer('category_id')->nullable();
$table->integer('kind_id')->nullable();
$table->timestamps();
});
}
Brands table:
public function up()
{
Schema::create('brands', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
Categories table:
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('category');
$table->timestamps();
});
}
Sizes table:
public function up()
{
Schema::create('sizes', function (Blueprint $table) {
$table->id();
$table->string('size');
$table->integer('product_id');
$table->timestamps();
});
}
Kinds table:
public function up()
{
Schema::create('kinds', function (Blueprint $table) {
$table->id();
$table->integer('product_id');
$table->string('kind');
$table->timestamps();
});
}
ProductImages table:
public function up()
{
Schema::create('product_images', function (Blueprint $table) {
$table->id();
$table->integer('product_id');
$table->string('url');
$table->timestamps();
});
}