Skip to content

Commit 7f5f1b6

Browse files
author
ahmadhuss
committed
feat: Product Model migration factory has been added
1 parent 4fafcab commit 7f5f1b6

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

app/Models/Product.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Product extends Model
9+
{
10+
use HasFactory;
11+
12+
protected $fillable = ['category_id', 'name', 'price', 'description'];
13+
14+
/**
15+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
16+
*/
17+
public function category(): \Illuminate\Database\Eloquent\Relations\BelongsTo
18+
{
19+
return $this->belongsTo(Category::class);
20+
}
21+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Database\Factories;
4+
5+
use App\Models\Product;
6+
use Illuminate\Database\Eloquent\Factories\Factory;
7+
8+
class ProductFactory extends Factory
9+
{
10+
/**
11+
* The name of the factory's corresponding model.
12+
*
13+
* @var string
14+
*/
15+
protected $model = Product::class;
16+
17+
/**
18+
* Define the model's default state.
19+
*
20+
* @return array
21+
*/
22+
public function definition()
23+
{
24+
return [
25+
'category_id' => mt_rand(1,12),
26+
'name' => $this->faker->word(),
27+
'price' => $this->faker->randomDigit(),
28+
'description' => $this->faker->paragraph(),
29+
30+
];
31+
}
32+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateProductsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('products', function (Blueprint $table) {
17+
$table->id();
18+
19+
// Foreign key
20+
$table->unsignedBigInteger('category_id');
21+
$table->foreign('category_id')->references('id')->on('categories');
22+
23+
$table->string('name');
24+
$table->decimal('price');
25+
$table->text('description');
26+
$table->timestamps();
27+
});
28+
}
29+
30+
/**
31+
* Reverse the migrations.
32+
*
33+
* @return void
34+
*/
35+
public function down()
36+
{
37+
Schema::dropIfExists('products');
38+
}
39+
}

database/seeders/DatabaseSeeder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Database\Seeders;
44

55
use App\Models\Category;
6+
use App\Models\Product;
67
use Illuminate\Database\Seeder;
78

89
class DatabaseSeeder extends Seeder
@@ -18,5 +19,6 @@ public function run()
1819
// If you don't want to save on the database
1920
// you can use `make()` instead of `create()`.
2021
Category::factory(12)->create();
22+
Product::factory(12)->create();
2123
}
2224
}

0 commit comments

Comments
 (0)