Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

David Orizu's avatar

Laravel 8 relationship three models belongs to Parent model

Hello everyone I'm currently working on a laravel project where I have a parent table that has the id's of three tables referenced to it. These table migrations also have their models respectively. Here are the table migrations files respectively:

create_products_table.php

        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('product_id', 10);
            $table->string('product_name');
            $table->string('image');
            $table->string('images');
            $table->string('product_description');
            $table->bigInteger('size_id')->unsigned();
            $table->string('color');
            $table->string('product_quantity');
            $table->string('old_price');
            $table->string('discount');
            $table->string('product_price');
            $table->bigInteger('user_id')->unsigned()->nullable();
            $table->bigInteger('category_id')->unsigned();
            $table->bigInteger('gender_id')->unsigned();
            $table->timestamps();
            $table->foreign('size_id')->references('id')->on('sizes')->onDelete('cascade');
            $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
            $table->foreign('gender_id')->references('id')->on('genders')->onDelete('cascade');
        });

create_genders_table.php

        Schema::create('genders', function (Blueprint $table) {
            $table->id();
            $table->string('gender_class');
            $table->timestamps();
        });

create_categories_table.php

        Schema::create('categories', function (Blueprint $table) {
            $table->id();
            $table->string('cat_name');
            $table->timestamps();
        });

create_sizes_table.php

        Schema::create('sizes', function (Blueprint $table) {
            $table->id();
            $table->string('sizes');
            $table->timestamps();
        });

Also this is how I defined the relationships on their models respectively

Product.php

    public function category()
    {
        return $this->hasMany(Category::class);
    }

    public function gender()
    {
        return $this->hasMany(Gender::class);
    }

    public function size()
    {
        return $this->hasMany(Size::class);
    }

Category.php

    public function product()
    {
        return $this->belongsTo(Product::class);
    }

Gender.php

    public function product()
    {
        return $this->belongsTo(Product::class);
    }

Size.php

    public function product()
    {
        return $this->belongsTo(Product::class);
    }

I'm actually a laravel beginner and I studied eloquent model relationships at laravel.com so what I did was just based on my understanding of one to many relationships. When I check all my request with dd($request), category_id, gender_id, size_id all show null and I believe it's because I didn't define the relationship properly. Now this is where I seriously need your assistance.

Also I'm trying do assign a random string of 10 integers to product_id and I used this line of code

$product->product_id = $faker->randomNumber(10);

When I check it with dd($faker) , nothing happens the page just reloads.

So please my experienced developers I seriously need your help I'll really be grateful if I get your replies today. Thanks in advance.

0 likes
10 replies
tykus's avatar

Your relations are wrong; if the _id foreign key is on products, then the Product belongs to the related model:

// Product.php
    public function category()
    {
        return $this->belongsTo(Category::class);
    }

    public function belongsTo()
    {
        return $this->hasMany(Gender::class);
    }

    public function belongsTo()
    {
        return $this->hasMany(Size::class);
    }
// Category.php
    public function products()
    {
        return $this->hasMany(Product::class);
    }
// Gender.php
    public function products()
    {
        return $this->hasMany(Product::class);
    }
// Size.php
    public function product()
    {
        return $this->hasMany(Product::class);
    }

Also I'm trying do assign a random string of 10 integers to product_id and I used this line of code

You can use a Str::random(10) to generate a random string.

Tippin's avatar

I think you are going about this backwards. Your parent model should not reference its children, rather, your children tables will have a column to reference the parent they belong to.

Schema::create('products', function (Blueprint $table) {
    $table->id();
    $table->string('product_id', 10); //?? Does this reference something else, or some ID from an external source
    $table->string('product_name');
    $table->string('image');
    $table->string('images');
    $table->string('product_description');
    $table->string('color');
    $table->string('product_quantity');
    $table->string('old_price');
    $table->string('discount');
    $table->string('product_price');
    $table->unsignedBigInteger('user_id')->nullable();
    $table->timestamps();
});

Schema::create('genders', function (Blueprint $table) {
    $table->id();
    $table->unsignedBigInteger('product_id');
    $table->string('gender_class');
    $table->timestamps();
    $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
});

Schema::create('categories', function (Blueprint $table) {
    $table->id();
    $table->unsignedBigInteger('product_id');
    $table->string('cat_name');
    $table->timestamps();
    $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
});

Schema::create('sizes', function (Blueprint $table) {
    $table->id();
    $table->unsignedBigInteger('product_id');
    $table->string('sizes');
    $table->timestamps();
    $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
});

Second, a hasmany relationship method name "should" be plural.

public function categories()
{
    return $this->hasMany(Category::class);
}

public function genders()
{
    return $this->hasMany(Gender::class);
}

public function sizes()
{
    return $this->hasMany(Size::class);
}

I would re-read the docs:

https://laravel.com/docs/8.x/eloquent-relationships#one-to-many

Edit: I think I misinterpreted your original intentions for the product linking to existing records.

newbie360's avatar

product gender ???? size ?? WoW

sometime product and category is via pivot table(manyToMany)

try use google sheet, mark down all column name of a table, and use different color link up the column of each table

tykus's avatar

@newbie360

can you try @tippin suggestion

So a Gender can belong to one product; a Size can belong to one product; a Category can belong to one product?

David Orizu's avatar

@newbie360 I love his suggestion but it would affect the work because that's not how I've pictured it to be like those id's have to be on the products table so that I can submit all the info together in one form.

tykus's avatar

@David Orizu

those id's have to be on the products table so that I can submit all the info together in one form

Not true.

The reason you need the foreign keys on the products table is because a Product belongs to a Category; a Product belongs to a Size; a Product belongs to a Gender. If this is not the case, then you need to describe the relationships appropriate to your application, and design the database accordingly.

Please or to participate in this conversation.