Sep 16, 2024
0
Level 3
Eloquent two key relationship with Product, User, and Category table with two categories only
Hi, here are my migrations files :
// users
public function up(): void
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('profile_image')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
// products
public function up(): void
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->foreignIdfor(\App\Models\Category::class);
$table->foreignIdFor(\App\Models\User::class);
$table->string('title');
$table->string('slug')->unique();
$table->text('description');
$table->string('media');
$table->boolean('visibility')->default(false);
$table->timestamps();
});
}
// categories
public function up(): void
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('slug')->unique();
$table->timestamps();
});
}
and here are my factories files :
// users factory
public function definition(): array
{
return [
'name' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'profile_image' => fake()->imageUrl(50, 50),
'email_verified_at' => now(),
'password' => static::$password ??= Hash::make('password'),
'remember_token' => Str::random(10),
];
}
// products factory
public function definition(): array
{
return [
'title' => fake()->text(15),
'slug' => fake()->slug(10, true),
'user_id' => \App\Models\User::factory(),
'category_id' => \App\Models\Category::factory(),
'description' => fake()->text(50),
'media' => fake()->imageUrl(150, 150),
'visibility' => fake()->boolean(false),
];
}
// categories
public function definition(): array
{
return [
'name' => fake()->randomElement(['Shoes', 'Shirts']),
'slug' => fake()->slug(),
];
}
The relationship between users and products works fine and with fake data I can do this in Tinker :
- $users = App\Models\Users::factory(10)->create(); that create 10 users.
- $products = App\Models\Product::factory()->count(20)->recycle($users)->create(); that create 20 products shared randomly between 10 users. But what I see is the products table is displayed like this :
products
+----+------------+----------------+--------------+-------------+
| id | name | category_id | user_id | ...
+----+------------+-----------------+----------+-------------+
| 1 | Polo Z | 1 | 5 | ... |
| 2 | T-sh100 | 2 | 8 | ... |
| 3 | Nike D | 3 | 4 | ... |
| 4 | Sport D | 4 | 1 | ... |
... ... ... ...
+----+------------+-------------+-------+---------------------+
and the categories table is like this :
categories
+----+------------+-------------+-------+
| id | name | slug | ...
+----+------------+-------------+-------+
| 1 | Shirts | shirts |
| 2 | Shoes | shoes |
+----+------------+-------------+-------+
The category_id increment by 1, 2, 3, 4... in products table instead of alternating between id 1 or 2. What I want it is to display it like this :
products
+----+------------+-------------+-------+-------------+
| id | name | category_id | user_id | ...
+----+------------+-------------+-------+-------------+
| 1 | Nike D | 2 | 5 |
| 2 | T-H100 | 1 | 8 |
| 3 | Sport R | 2 | 4 |
| 4 | Polo Z | 1 | 1 |
... ... ... ...
+----+------------+-------------+-------+-------------+
What I missed in my schema design?
Please or to participate in this conversation.