diff options
-rw-r--r-- | workbench/app/Livewire/CategoriesTable.php | 6 | ||||
-rw-r--r-- | workbench/app/Models/Category.php | 22 | ||||
-rw-r--r-- | workbench/database/factories/CategoryFactory.php | 33 | ||||
-rw-r--r-- | workbench/database/migrations/2025_03_27_211321_create_categories_table.php | 38 |
4 files changed, 95 insertions, 4 deletions
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 @@ +<?php + +namespace Workbench\App\Models; + +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Model; + +class Category extends Model +{ + use HasFactory; + + protected static function newFactory() + { + return \Workbench\Database\Factories\CategoryFactory::new(); + } + + public function products(): HasMany + { + return $this->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 @@ +<?php + +namespace Workbench\Database\Factories; + +use Illuminate\Database\Eloquent\Factories\Factory; +use Workbench\App\Models\Category; + +/** + * @template TModel of \Workbench\App\Models\Category + * + * @extends \Illuminate\Database\Eloquent\Factories\Factory<TModel> + */ +class CategoryFactory extends Factory +{ + /** + * The name of the factory's corresponding model. + * + * @var class-string<TModel> + */ + protected $model = Category::class; + + /** + * Define the model's default state. + * + * @return array<string, mixed> + */ + 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 @@ +<?php + +use Illuminate\Database\Migrations\Migration; +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Facades\Schema; + +return new class extends Migration +{ + /** + * Run the migrations. + */ + public function up(): void + { + Schema::create('categories', function (Blueprint $table) { + $table->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'); + }); + + } +}; |