From 80a183a94bd344c3ac08109019fdc7d341a57a6e Mon Sep 17 00:00:00 2001 From: Sam Light Date: Fri, 28 Mar 2025 01:07:59 +0000 Subject: Setup categories table, model and factory --- workbench/app/Livewire/CategoriesTable.php | 6 ++-- workbench/app/Models/Category.php | 22 +++++++++++++ workbench/database/factories/CategoryFactory.php | 33 +++++++++++++++++++ .../2025_03_27_211321_create_categories_table.php | 38 ++++++++++++++++++++++ 4 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 workbench/app/Models/Category.php create mode 100644 workbench/database/factories/CategoryFactory.php create mode 100644 workbench/database/migrations/2025_03_27_211321_create_categories_table.php (limited to 'workbench') diff --git a/workbench/app/Livewire/CategoriesTable.php b/workbench/app/Livewire/CategoriesTable.php index 14cd5a8..722385f 100644 --- a/workbench/app/Livewire/CategoriesTable.php +++ b/workbench/app/Livewire/CategoriesTable.php @@ -2,21 +2,19 @@ namespace Workbench\App\Livewire; -use Workbench\App\Models\Product; +use Workbench\App\Models\Category; use Lightscale\LaralightTables\Columns\Column; class CategoriesTable extends Table { - protected $model = Product::class; + protected $model = Category::class; public function columns(): array { return [ Column::make('id', 'ID'), Column::make('name', 'Name'), - Column::make('price', 'Price'), - Column::make('stock', 'Stock'), ]; } } diff --git a/workbench/app/Models/Category.php b/workbench/app/Models/Category.php new file mode 100644 index 0000000..d1b1dcd --- /dev/null +++ b/workbench/app/Models/Category.php @@ -0,0 +1,22 @@ +hasMany(Product::class); + } +} diff --git a/workbench/database/factories/CategoryFactory.php b/workbench/database/factories/CategoryFactory.php new file mode 100644 index 0000000..756c22a --- /dev/null +++ b/workbench/database/factories/CategoryFactory.php @@ -0,0 +1,33 @@ + + */ +class CategoryFactory extends Factory +{ + /** + * The name of the factory's corresponding model. + * + * @var class-string + */ + protected $model = Category::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'name' => fake()->text(20), + ]; + } +} diff --git a/workbench/database/migrations/2025_03_27_211321_create_categories_table.php b/workbench/database/migrations/2025_03_27_211321_create_categories_table.php new file mode 100644 index 0000000..5a75f30 --- /dev/null +++ b/workbench/database/migrations/2025_03_27_211321_create_categories_table.php @@ -0,0 +1,38 @@ +id(); + $table->string('name'); + $table->timestamps(); + $table->softDeletes(); + }); + + Schema::table('products', function (Blueprint $table) { + $table->foreignId('category_id'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('categories'); + + Schema::table('products', function (Blueprint $table) { + $table->dropColumn('category_id'); + }); + + } +}; -- cgit v1.2.3