Skip to content

Commit 191ffc7

Browse files
committed
feat: add Product resource with translatable attributes
1 parent 92e93b0 commit 191ffc7

File tree

12 files changed

+468
-3
lines changed

12 files changed

+468
-3
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@
4747
"bezhansalleh/filament-shield": "^3.3",
4848
"datalinx/php-utils": "^2.5",
4949
"filament/filament": "^3.3",
50-
"spatie/laravel-package-tools": "^1.19"
50+
"filament/spatie-laravel-translatable-plugin": "^3.2",
51+
"spatie/laravel-package-tools": "^1.19",
52+
"spatie/laravel-translatable": "^6.11"
5153
},
5254
"require-dev": {
5355
"laravel/pint": "^1.21",
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Eclipse\Catalogue\Factories;
4+
5+
use Eclipse\Catalogue\Models\Product;
6+
use Illuminate\Database\Eloquent\Factories\Factory;
7+
use Illuminate\Support\Carbon;
8+
9+
class ProductFactory extends Factory
10+
{
11+
protected $model = Product::class;
12+
13+
public function definition(): array
14+
{
15+
$englishName = mb_ucfirst($this->faker->words(3, true));
16+
$slovenianName = 'SI: '.$englishName;
17+
18+
$englishShortDesc = $this->faker->sentence();
19+
$slovenianShortDesc = 'SI: '.$englishShortDesc;
20+
21+
$englishDesc = $this->faker->paragraphs(3, true);
22+
$slovenianDesc = 'SI: '.$englishDesc;
23+
24+
return [
25+
'code' => $this->faker->numerify('######'),
26+
'barcode' => $this->faker->ean13(),
27+
'manufacturers_code' => $this->faker->bothify('MFR-####???'),
28+
'suppliers_code' => $this->faker->bothify('SUP-####???'),
29+
'net_weight' => $this->faker->randomFloat(2, 0.1, 100),
30+
'gross_weight' => $this->faker->randomFloat(2, 0.1, 100),
31+
'name' => [
32+
'en' => $englishName,
33+
'sl' => $slovenianName,
34+
],
35+
'short_description' => [
36+
'en' => $englishShortDesc,
37+
'sl' => $slovenianShortDesc,
38+
],
39+
'description' => [
40+
'en' => $englishDesc,
41+
'sl' => $slovenianDesc,
42+
],
43+
'created_at' => Carbon::now(),
44+
'updated_at' => Carbon::now(),
45+
];
46+
}
47+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
public function up(): void
10+
{
11+
Schema::create('catalogue_products', function (Blueprint $table) {
12+
$table->id();
13+
$table->string('code')->nullable();
14+
$table->string('barcode')->nullable();
15+
$table->string('manufacturers_code')->nullable();
16+
$table->string('suppliers_code')->nullable();
17+
$table->decimal('net_weight')->nullable();
18+
$table->decimal('gross_weight')->nullable();
19+
$table->json('name')->nullable();
20+
$table->json('short_description')->nullable();
21+
$table->json('description')->nullable();
22+
$table->timestamps();
23+
$table->softDeletes();
24+
});
25+
}
26+
27+
public function down(): void
28+
{
29+
Schema::dropIfExists('catalogue_products');
30+
}
31+
};

database/seeders/ProductSeeder.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Eclipse\Catalogue\Seeders;
4+
5+
use Eclipse\Catalogue\Models\Product;
6+
use Illuminate\Database\Seeder;
7+
8+
class ProductSeeder extends Seeder
9+
{
10+
public function run(): void
11+
{
12+
Product::factory()
13+
->count(100)
14+
->create();
15+
}
16+
}

src/CataloguePlugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function getId(): string
1414

1515
public function register(Panel $panel): void
1616
{
17-
//
17+
$panel->discoverResources(__DIR__.'/Filament/Resources', 'Eclipse\\Catalogue\\Filament\\Resources');
1818
}
1919

2020
public function boot(Panel $panel): void

src/CatalogueServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public function configurePackage(Package $package): void
1313
{
1414
$package->name(static::$name)
1515
->hasConfigFile()
16-
->hasTranslations();
16+
->hasTranslations()
17+
->discoversMigrations()
18+
->runsMigrations();
1719
}
1820
}
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<?php
2+
3+
namespace Eclipse\Catalogue\Filament\Resources;
4+
5+
use BezhanSalleh\FilamentShield\Contracts\HasShieldPermissions;
6+
use Eclipse\Catalogue\Filament\Resources\ProductResource\Pages;
7+
use Eclipse\Catalogue\Models\Product;
8+
use Filament\Forms\Components\Placeholder;
9+
use Filament\Forms\Components\RichEditor;
10+
use Filament\Forms\Components\TextInput;
11+
use Filament\Forms\Form;
12+
use Filament\Resources\Concerns\Translatable;
13+
use Filament\Resources\Resource;
14+
use Filament\Tables\Actions\BulkActionGroup;
15+
use Filament\Tables\Actions\DeleteAction;
16+
use Filament\Tables\Actions\DeleteBulkAction;
17+
use Filament\Tables\Actions\EditAction;
18+
use Filament\Tables\Actions\ForceDeleteAction;
19+
use Filament\Tables\Actions\ForceDeleteBulkAction;
20+
use Filament\Tables\Actions\RestoreAction;
21+
use Filament\Tables\Actions\RestoreBulkAction;
22+
use Filament\Tables\Columns\TextColumn;
23+
use Filament\Tables\Filters\TrashedFilter;
24+
use Filament\Tables\Table;
25+
use Illuminate\Database\Eloquent\Builder;
26+
use Illuminate\Database\Eloquent\SoftDeletingScope;
27+
28+
class ProductResource extends Resource implements HasShieldPermissions
29+
{
30+
use Translatable;
31+
32+
protected static ?string $model = Product::class;
33+
34+
protected static ?string $slug = 'products';
35+
36+
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
37+
38+
protected static ?string $navigationGroup = 'Catalogue';
39+
40+
public static function form(Form $form): Form
41+
{
42+
return $form
43+
->schema([
44+
TextInput::make('code'),
45+
46+
TextInput::make('barcode')
47+
->maxLength(255),
48+
49+
TextInput::make('manufacturers_code')
50+
->label('Manufacturer\'s Code')
51+
->maxLength(255),
52+
53+
TextInput::make('suppliers_code')
54+
->label('Supplier\'s Code')
55+
->maxLength(255),
56+
57+
TextInput::make('net_weight')
58+
->numeric(),
59+
60+
TextInput::make('gross_weight')
61+
->numeric(),
62+
63+
TextInput::make('name'),
64+
65+
TextInput::make('short_description'),
66+
67+
RichEditor::make('description'),
68+
69+
Placeholder::make('created_at')
70+
->label('Created Date')
71+
->content(fn (?Product $record): string => $record?->created_at?->diffForHumans() ?? '-'),
72+
73+
Placeholder::make('updated_at')
74+
->label('Last Modified Date')
75+
->content(fn (?Product $record): string => $record?->updated_at?->diffForHumans() ?? '-'),
76+
]);
77+
}
78+
79+
public static function table(Table $table): Table
80+
{
81+
return $table
82+
->columns([
83+
TextColumn::make('id'),
84+
85+
TextColumn::make('name')
86+
->toggleable(false),
87+
88+
TextColumn::make('short_description')
89+
->words(5),
90+
91+
TextColumn::make('code')
92+
->copyable(),
93+
94+
TextColumn::make('barcode'),
95+
96+
TextColumn::make('manufacturers_code'),
97+
98+
TextColumn::make('suppliers_code'),
99+
100+
TextColumn::make('net_weight')
101+
->numeric(3)
102+
->suffix(' kg'),
103+
104+
TextColumn::make('gross_weight')
105+
->numeric(3)
106+
->suffix(' kg'),
107+
])
108+
->filters([
109+
TrashedFilter::make(),
110+
])
111+
->actions([
112+
EditAction::make(),
113+
DeleteAction::make(),
114+
RestoreAction::make(),
115+
ForceDeleteAction::make(),
116+
])
117+
->bulkActions([
118+
BulkActionGroup::make([
119+
DeleteBulkAction::make(),
120+
RestoreBulkAction::make(),
121+
ForceDeleteBulkAction::make(),
122+
]),
123+
]);
124+
}
125+
126+
public static function getPages(): array
127+
{
128+
return [
129+
'index' => Pages\ListProducts::route('/'),
130+
'create' => Pages\CreateProduct::route('/create'),
131+
'edit' => Pages\EditProduct::route('/{record}/edit'),
132+
];
133+
}
134+
135+
public static function getEloquentQuery(): Builder
136+
{
137+
return parent::getEloquentQuery()
138+
->withoutGlobalScopes([
139+
SoftDeletingScope::class,
140+
]);
141+
}
142+
143+
public static function getGloballySearchableAttributes(): array
144+
{
145+
return [];
146+
}
147+
148+
public static function getPermissionPrefixes(): array
149+
{
150+
return [
151+
'view_any',
152+
'view',
153+
'create',
154+
'update',
155+
'restore',
156+
'restore_any',
157+
'delete',
158+
'delete_any',
159+
'force_delete',
160+
'force_delete_any',
161+
];
162+
}
163+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Eclipse\Catalogue\Filament\Resources\ProductResource\Pages;
4+
5+
use Eclipse\Catalogue\Filament\Resources\ProductResource;
6+
use Filament\Actions;
7+
use Filament\Resources\Pages\CreateRecord;
8+
9+
class CreateProduct extends CreateRecord
10+
{
11+
use CreateRecord\Concerns\Translatable;
12+
13+
protected static string $resource = ProductResource::class;
14+
15+
protected function getHeaderActions(): array
16+
{
17+
return [
18+
Actions\LocaleSwitcher::make(),
19+
];
20+
}
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Eclipse\Catalogue\Filament\Resources\ProductResource\Pages;
4+
5+
use Eclipse\Catalogue\Filament\Resources\ProductResource;
6+
use Filament\Actions;
7+
use Filament\Resources\Pages\EditRecord;
8+
9+
class EditProduct extends EditRecord
10+
{
11+
use EditRecord\Concerns\Translatable;
12+
13+
protected static string $resource = ProductResource::class;
14+
15+
protected function getHeaderActions(): array
16+
{
17+
return [
18+
Actions\LocaleSwitcher::make(),
19+
Actions\DeleteAction::make(),
20+
Actions\ForceDeleteAction::make(),
21+
Actions\RestoreAction::make(),
22+
];
23+
}
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Eclipse\Catalogue\Filament\Resources\ProductResource\Pages;
4+
5+
use Eclipse\Catalogue\Filament\Resources\ProductResource;
6+
use Filament\Actions;
7+
use Filament\Resources\Pages\ListRecords;
8+
9+
class ListProducts extends ListRecords
10+
{
11+
use ListRecords\Concerns\Translatable;
12+
13+
protected static string $resource = ProductResource::class;
14+
15+
protected function getHeaderActions(): array
16+
{
17+
return [
18+
Actions\LocaleSwitcher::make(),
19+
Actions\CreateAction::make(),
20+
];
21+
}
22+
}

0 commit comments

Comments
 (0)