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.
The relationship on your Product model has to be a HasOne relation and not a HasMany relation. I think that fixes your relationship bug.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community