Add CatalogPlugin scaffold and EvCompany resource (T2.0, T2.1)
Registers CatalogPlugin with the admin panel so catalog Filament resources auto-discover, and adds the EvCompany model/migration/ factory plus its Filament resource (auto-generated slug on create).
This commit is contained in:
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog\Database\Factories;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
use Modules\Catalog\Models\EvCompany;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends Factory<EvCompany>
|
||||||
|
*/
|
||||||
|
class EvCompanyFactory extends Factory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Define the model's default state.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'name' => fake()->company(),
|
||||||
|
'mm_name' => fake()->company(),
|
||||||
|
'slug' => fake()->unique()->slug(),
|
||||||
|
'description' => fake()->sentence(),
|
||||||
|
'mm_description' => null,
|
||||||
|
'contact' => fake()->phoneNumber(),
|
||||||
|
'address' => fake()->address(),
|
||||||
|
'logo' => null,
|
||||||
|
'is_active' => true,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
+36
@@ -0,0 +1,36 @@
|
|||||||
|
<?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('ev_companies', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('name');
|
||||||
|
$table->string('mm_name');
|
||||||
|
$table->string('slug')->unique();
|
||||||
|
$table->text('description')->nullable();
|
||||||
|
$table->text('mm_description')->nullable();
|
||||||
|
$table->string('contact');
|
||||||
|
$table->string('address');
|
||||||
|
$table->string('logo')->nullable();
|
||||||
|
$table->boolean('is_active')->default(true);
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('ev_companies');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog;
|
||||||
|
|
||||||
|
use Filament\Contracts\Plugin;
|
||||||
|
use Filament\Panel;
|
||||||
|
|
||||||
|
class CatalogPlugin implements Plugin
|
||||||
|
{
|
||||||
|
public function getId(): string
|
||||||
|
{
|
||||||
|
return 'catalog';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function register(Panel $panel): void
|
||||||
|
{
|
||||||
|
$panel
|
||||||
|
->discoverResources(
|
||||||
|
in: __DIR__.'/Filament/Resources',
|
||||||
|
for: 'Modules\Catalog\Filament\Resources',
|
||||||
|
)
|
||||||
|
->discoverPages(
|
||||||
|
in: __DIR__.'/Filament/Pages',
|
||||||
|
for: 'Modules\Catalog\Filament\Pages',
|
||||||
|
)
|
||||||
|
->discoverWidgets(
|
||||||
|
in: __DIR__.'/Filament/Widgets',
|
||||||
|
for: 'Modules\Catalog\Filament\Widgets',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function boot(Panel $panel): void {}
|
||||||
|
|
||||||
|
public static function make(): static
|
||||||
|
{
|
||||||
|
return app(static::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog\Filament\Resources\EvCompanies;
|
||||||
|
|
||||||
|
use BackedEnum;
|
||||||
|
use Filament\Resources\Resource;
|
||||||
|
use Filament\Schemas\Schema;
|
||||||
|
use Filament\Support\Icons\Heroicon;
|
||||||
|
use Filament\Tables\Table;
|
||||||
|
use Modules\Catalog\Filament\Resources\EvCompanies\Pages\CreateEvCompany;
|
||||||
|
use Modules\Catalog\Filament\Resources\EvCompanies\Pages\EditEvCompany;
|
||||||
|
use Modules\Catalog\Filament\Resources\EvCompanies\Pages\ListEvCompanies;
|
||||||
|
use Modules\Catalog\Filament\Resources\EvCompanies\Schemas\EvCompanyForm;
|
||||||
|
use Modules\Catalog\Filament\Resources\EvCompanies\Tables\EvCompaniesTable;
|
||||||
|
use Modules\Catalog\Models\EvCompany;
|
||||||
|
use UnitEnum;
|
||||||
|
|
||||||
|
class EvCompanyResource extends Resource
|
||||||
|
{
|
||||||
|
protected static ?string $model = EvCompany::class;
|
||||||
|
|
||||||
|
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedRectangleStack;
|
||||||
|
|
||||||
|
protected static string|UnitEnum|null $navigationGroup = 'Catalog';
|
||||||
|
|
||||||
|
public static function form(Schema $schema): Schema
|
||||||
|
{
|
||||||
|
return EvCompanyForm::configure($schema);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function table(Table $table): Table
|
||||||
|
{
|
||||||
|
return EvCompaniesTable::configure($table);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getRelations(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
//
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getPages(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'index' => ListEvCompanies::route('/'),
|
||||||
|
'create' => CreateEvCompany::route('/create'),
|
||||||
|
'edit' => EditEvCompany::route('/{record}/edit'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog\Filament\Resources\EvCompanies\Pages;
|
||||||
|
|
||||||
|
use Filament\Resources\Pages\CreateRecord;
|
||||||
|
use Modules\Catalog\Filament\Resources\EvCompanies\EvCompanyResource;
|
||||||
|
|
||||||
|
class CreateEvCompany extends CreateRecord
|
||||||
|
{
|
||||||
|
protected static string $resource = EvCompanyResource::class;
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog\Filament\Resources\EvCompanies\Pages;
|
||||||
|
|
||||||
|
use Filament\Actions\DeleteAction;
|
||||||
|
use Filament\Resources\Pages\EditRecord;
|
||||||
|
use Modules\Catalog\Filament\Resources\EvCompanies\EvCompanyResource;
|
||||||
|
|
||||||
|
class EditEvCompany extends EditRecord
|
||||||
|
{
|
||||||
|
protected static string $resource = EvCompanyResource::class;
|
||||||
|
|
||||||
|
protected function getHeaderActions(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
DeleteAction::make(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog\Filament\Resources\EvCompanies\Pages;
|
||||||
|
|
||||||
|
use Filament\Actions\CreateAction;
|
||||||
|
use Filament\Resources\Pages\ListRecords;
|
||||||
|
use Modules\Catalog\Filament\Resources\EvCompanies\EvCompanyResource;
|
||||||
|
|
||||||
|
class ListEvCompanies extends ListRecords
|
||||||
|
{
|
||||||
|
protected static string $resource = EvCompanyResource::class;
|
||||||
|
|
||||||
|
protected function getHeaderActions(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
CreateAction::make(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog\Filament\Resources\EvCompanies\Schemas;
|
||||||
|
|
||||||
|
use Filament\Forms\Components\FileUpload;
|
||||||
|
use Filament\Forms\Components\Textarea;
|
||||||
|
use Filament\Forms\Components\TextInput;
|
||||||
|
use Filament\Forms\Components\Toggle;
|
||||||
|
use Filament\Schemas\Schema;
|
||||||
|
|
||||||
|
class EvCompanyForm
|
||||||
|
{
|
||||||
|
public static function configure(Schema $schema): Schema
|
||||||
|
{
|
||||||
|
return $schema
|
||||||
|
->components([
|
||||||
|
TextInput::make('name')
|
||||||
|
->required()
|
||||||
|
->maxLength(255),
|
||||||
|
TextInput::make('mm_name')
|
||||||
|
->required()
|
||||||
|
->maxLength(255),
|
||||||
|
Textarea::make('description')
|
||||||
|
->columnSpanFull(),
|
||||||
|
Textarea::make('mm_description')
|
||||||
|
->columnSpanFull(),
|
||||||
|
TextInput::make('contact')
|
||||||
|
->required()
|
||||||
|
->tel()
|
||||||
|
->maxLength(255),
|
||||||
|
TextInput::make('address')
|
||||||
|
->required()
|
||||||
|
->maxLength(255),
|
||||||
|
FileUpload::make('logo')
|
||||||
|
->image(),
|
||||||
|
Toggle::make('is_active')
|
||||||
|
->required()
|
||||||
|
->default(true),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog\Filament\Resources\EvCompanies\Tables;
|
||||||
|
|
||||||
|
use Filament\Actions\BulkActionGroup;
|
||||||
|
use Filament\Actions\DeleteBulkAction;
|
||||||
|
use Filament\Actions\EditAction;
|
||||||
|
use Filament\Tables\Columns\IconColumn;
|
||||||
|
use Filament\Tables\Columns\ImageColumn;
|
||||||
|
use Filament\Tables\Columns\TextColumn;
|
||||||
|
use Filament\Tables\Filters\TernaryFilter;
|
||||||
|
use Filament\Tables\Table;
|
||||||
|
|
||||||
|
class EvCompaniesTable
|
||||||
|
{
|
||||||
|
public static function configure(Table $table): Table
|
||||||
|
{
|
||||||
|
return $table
|
||||||
|
->columns([
|
||||||
|
ImageColumn::make('logo')
|
||||||
|
->circular(),
|
||||||
|
TextColumn::make('name')
|
||||||
|
->searchable()
|
||||||
|
->sortable(),
|
||||||
|
TextColumn::make('slug')
|
||||||
|
->searchable(),
|
||||||
|
TextColumn::make('contact')
|
||||||
|
->searchable(),
|
||||||
|
IconColumn::make('is_active')
|
||||||
|
->boolean(),
|
||||||
|
TextColumn::make('created_at')
|
||||||
|
->dateTime()
|
||||||
|
->sortable()
|
||||||
|
->toggleable(isToggledHiddenByDefault: true),
|
||||||
|
])
|
||||||
|
->defaultSort('created_at', 'desc')
|
||||||
|
->filters([
|
||||||
|
TernaryFilter::make('is_active'),
|
||||||
|
])
|
||||||
|
->recordActions([
|
||||||
|
EditAction::make(),
|
||||||
|
])
|
||||||
|
->toolbarActions([
|
||||||
|
BulkActionGroup::make([
|
||||||
|
DeleteBulkAction::make(),
|
||||||
|
]),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\Catalog\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use Modules\Catalog\Database\Factories\EvCompanyFactory;
|
||||||
|
|
||||||
|
class EvCompany extends Model
|
||||||
|
{
|
||||||
|
/** @use HasFactory<EvCompanyFactory> */
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var list<string>
|
||||||
|
*/
|
||||||
|
protected $fillable = [
|
||||||
|
'name',
|
||||||
|
'mm_name',
|
||||||
|
'slug',
|
||||||
|
'description',
|
||||||
|
'mm_description',
|
||||||
|
'contact',
|
||||||
|
'address',
|
||||||
|
'logo',
|
||||||
|
'is_active',
|
||||||
|
];
|
||||||
|
|
||||||
|
protected static function booted(): void
|
||||||
|
{
|
||||||
|
static::creating(function (self $evCompany): void {
|
||||||
|
if (filled($evCompany->slug)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$slug = Str::slug($evCompany->name);
|
||||||
|
$uniqueSlug = $slug;
|
||||||
|
$suffix = 1;
|
||||||
|
|
||||||
|
while (static::where('slug', $uniqueSlug)->exists()) {
|
||||||
|
$uniqueSlug = "{$slug}-{$suffix}";
|
||||||
|
$suffix++;
|
||||||
|
}
|
||||||
|
|
||||||
|
$evCompany->slug = $uniqueSlug;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, string>
|
||||||
|
*/
|
||||||
|
protected function casts(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'is_active' => 'boolean',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use Livewire\Livewire;
|
||||||
|
use Modules\Catalog\Filament\Resources\EvCompanies\Pages\CreateEvCompany;
|
||||||
|
use Modules\Catalog\Filament\Resources\EvCompanies\Pages\EditEvCompany;
|
||||||
|
use Modules\Catalog\Filament\Resources\EvCompanies\Pages\ListEvCompanies;
|
||||||
|
use Modules\Catalog\Models\EvCompany;
|
||||||
|
use Spatie\Permission\Models\Role;
|
||||||
|
|
||||||
|
use function Pest\Laravel\assertDatabaseHas;
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
Role::findOrCreate('admin', 'web');
|
||||||
|
|
||||||
|
$this->admin = User::factory()->create()->assignRole('admin');
|
||||||
|
$this->actingAs($this->admin);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('can list ev companies', function () {
|
||||||
|
$companies = EvCompany::factory()->count(3)->create();
|
||||||
|
|
||||||
|
Livewire::test(ListEvCompanies::class)
|
||||||
|
->assertOk()
|
||||||
|
->assertCanSeeTableRecords($companies);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('can create an ev company', function () {
|
||||||
|
$company = EvCompany::factory()->make();
|
||||||
|
|
||||||
|
Livewire::test(CreateEvCompany::class)
|
||||||
|
->fillForm([
|
||||||
|
'name' => $company->name,
|
||||||
|
'mm_name' => $company->mm_name,
|
||||||
|
'contact' => $company->contact,
|
||||||
|
'address' => $company->address,
|
||||||
|
'is_active' => true,
|
||||||
|
])
|
||||||
|
->call('create')
|
||||||
|
->assertNotified()
|
||||||
|
->assertRedirect();
|
||||||
|
|
||||||
|
assertDatabaseHas(EvCompany::class, [
|
||||||
|
'name' => $company->name,
|
||||||
|
'slug' => Str::slug($company->name),
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('can edit an ev company', function () {
|
||||||
|
$company = EvCompany::factory()->create();
|
||||||
|
|
||||||
|
Livewire::test(EditEvCompany::class, ['record' => $company->getRouteKey()])
|
||||||
|
->assertOk()
|
||||||
|
->fillForm(['name' => 'Updated Name'])
|
||||||
|
->call('save')
|
||||||
|
->assertNotified();
|
||||||
|
|
||||||
|
assertDatabaseHas(EvCompany::class, [
|
||||||
|
'id' => $company->id,
|
||||||
|
'name' => 'Updated Name',
|
||||||
|
]);
|
||||||
|
});
|
||||||
@@ -19,6 +19,7 @@ use Illuminate\Foundation\Http\Middleware\PreventRequestForgery;
|
|||||||
use Illuminate\Routing\Middleware\SubstituteBindings;
|
use Illuminate\Routing\Middleware\SubstituteBindings;
|
||||||
use Illuminate\Session\Middleware\StartSession;
|
use Illuminate\Session\Middleware\StartSession;
|
||||||
use Illuminate\View\Middleware\ShareErrorsFromSession;
|
use Illuminate\View\Middleware\ShareErrorsFromSession;
|
||||||
|
use Modules\Catalog\CatalogPlugin;
|
||||||
|
|
||||||
class AdminPanelProvider extends PanelProvider
|
class AdminPanelProvider extends PanelProvider
|
||||||
{
|
{
|
||||||
@@ -38,7 +39,9 @@ class AdminPanelProvider extends PanelProvider
|
|||||||
NavigationGroup::make()->label('Operations'),
|
NavigationGroup::make()->label('Operations'),
|
||||||
NavigationGroup::make()->label('Access'),
|
NavigationGroup::make()->label('Access'),
|
||||||
])
|
])
|
||||||
->plugins([])
|
->plugins([
|
||||||
|
CatalogPlugin::make(),
|
||||||
|
])
|
||||||
->discoverResources(in: app_path('Filament/Resources'), for: 'App\Filament\Resources')
|
->discoverResources(in: app_path('Filament/Resources'), for: 'App\Filament\Resources')
|
||||||
->discoverPages(in: app_path('Filament/Pages'), for: 'App\Filament\Pages')
|
->discoverPages(in: app_path('Filament/Pages'), for: 'App\Filament\Pages')
|
||||||
->pages([
|
->pages([
|
||||||
|
|||||||
Reference in New Issue
Block a user