From c22677088acbeb812ffd469118bdd63d25350e0e Mon Sep 17 00:00:00 2001 From: QuocTuanIT Date: Sun, 15 Oct 2023 17:17:57 +0700 Subject: [PATCH] Creating Tags for Customers --- app/Filament/Resources/CustomerResource.php | 9 +- app/Filament/Resources/TagResource.php | 98 +++++++++++++++++++ .../Resources/TagResource/Pages/CreateTag.php | 12 +++ .../Resources/TagResource/Pages/EditTag.php | 19 ++++ .../Resources/TagResource/Pages/ListTags.php | 19 ++++ app/Models/Customer.php | 6 ++ app/Models/Tag.php | 19 ++++ .../2023_10_15_100559_create_tags_table.php | 29 ++++++ .../2023_10_15_100745_create_customer_tag.php | 29 ++++++ database/seeders/DatabaseSeeder.php | 9 ++ resources/views/customer/tagsList.blade.php | 8 ++ 11 files changed, 256 insertions(+), 1 deletion(-) create mode 100644 app/Filament/Resources/TagResource.php create mode 100644 app/Filament/Resources/TagResource/Pages/CreateTag.php create mode 100644 app/Filament/Resources/TagResource/Pages/EditTag.php create mode 100644 app/Filament/Resources/TagResource/Pages/ListTags.php create mode 100644 app/Models/Tag.php create mode 100644 database/migrations/2023_10_15_100559_create_tags_table.php create mode 100644 database/migrations/2023_10_15_100745_create_customer_tag.php create mode 100644 resources/views/customer/tagsList.blade.php diff --git a/app/Filament/Resources/CustomerResource.php b/app/Filament/Resources/CustomerResource.php index 6397729..f276b28 100644 --- a/app/Filament/Resources/CustomerResource.php +++ b/app/Filament/Resources/CustomerResource.php @@ -37,6 +37,10 @@ public static function form(Form $form): Form ->columnSpanFull(), Forms\Components\Select::make('lead_source_id') ->relationship('leadSource', 'name'), + Forms\Components\Select::make('tags') + ->relationship('tags', 'name') + ->preload() + ->multiple(), ]); } @@ -47,8 +51,11 @@ public static function table(Table $table): Table Tables\Columns\TextColumn::make('first_name') ->label('Name') ->formatStateUsing(function ($record) { - return $record->first_name . ' ' . $record->last_name; + $tagsList = view('customer.tagsList', ['tags' => $record->tags])->render(); + + return $record->first_name . ' ' . $record->last_name . ' ' . $tagsList; }) + ->html() ->searchable(['first_name', 'last_name']), Tables\Columns\TextColumn::make('email') ->searchable(), diff --git a/app/Filament/Resources/TagResource.php b/app/Filament/Resources/TagResource.php new file mode 100644 index 0000000..8cdb8d0 --- /dev/null +++ b/app/Filament/Resources/TagResource.php @@ -0,0 +1,98 @@ +schema([ + Forms\Components\TextInput::make('name') + ->required() + ->maxLength(255), + Forms\Components\ColorPicker::make('color') + ]); + } + + public static function table(Table $table): Table + { + return $table + ->columns([ + Tables\Columns\TextColumn::make('name') + ->searchable(), + Tables\Columns\ColorColumn::make('color') + ->searchable(), + Tables\Columns\TextColumn::make('created_at') + ->dateTime() + ->sortable() + ->toggleable(isToggledHiddenByDefault: true), + Tables\Columns\TextColumn::make('updated_at') + ->dateTime() + ->sortable() + ->toggleable(isToggledHiddenByDefault: true), + ]) + ->filters([ + // + ]) + ->actions([ + Tables\Actions\EditAction::make(), + Tables\Actions\DeleteAction::make() + ->action(function ($data, $record) { + if ($record->customers()->count() > 0) { + Notification::make() + ->danger() + ->title('Tag is in use') + ->body('Tag is in use by customers.') + ->send(); + + return; + } + + Notification::make() + ->success() + ->title('Tag deleted') + ->body('Tag has been deleted.') + ->send(); + + $record->delete(); + }) + ]) + ->bulkActions([ + Tables\Actions\BulkActionGroup::make([ + Tables\Actions\DeleteBulkAction::make(), + ]), + ]); + } + + public static function getRelations(): array + { + return [ + // + ]; + } + + public static function getPages(): array + { + return [ + 'index' => Pages\ListTags::route('/'), + 'create' => Pages\CreateTag::route('/create'), + 'edit' => Pages\EditTag::route('/{record}/edit'), + ]; + } +} diff --git a/app/Filament/Resources/TagResource/Pages/CreateTag.php b/app/Filament/Resources/TagResource/Pages/CreateTag.php new file mode 100644 index 0000000..f10f4d9 --- /dev/null +++ b/app/Filament/Resources/TagResource/Pages/CreateTag.php @@ -0,0 +1,12 @@ +belongsTo(LeadSource::class); } + + public function tags(): BelongsToMany + { + return $this->belongsToMany(Tag::class); + } } diff --git a/app/Models/Tag.php b/app/Models/Tag.php new file mode 100644 index 0000000..e13c564 --- /dev/null +++ b/app/Models/Tag.php @@ -0,0 +1,19 @@ +belongsToMany(Customer::class); + } +} diff --git a/database/migrations/2023_10_15_100559_create_tags_table.php b/database/migrations/2023_10_15_100559_create_tags_table.php new file mode 100644 index 0000000..95c3de3 --- /dev/null +++ b/database/migrations/2023_10_15_100559_create_tags_table.php @@ -0,0 +1,29 @@ +id(); + $table->string('name'); + $table->string('color')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('tags'); + } +}; diff --git a/database/migrations/2023_10_15_100745_create_customer_tag.php b/database/migrations/2023_10_15_100745_create_customer_tag.php new file mode 100644 index 0000000..56e3a96 --- /dev/null +++ b/database/migrations/2023_10_15_100745_create_customer_tag.php @@ -0,0 +1,29 @@ +foreignIdFor(Customer::class)->constrained(); + $table->foreignIdFor(Tag::class)->constrained(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('customer_tag'); + } +}; diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index f2d40dc..07dd67b 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -39,5 +39,14 @@ public function run(): void \App\Models\Customer::factory() ->count(10) ->create(); + + $tags = [ + 'Priority', + 'VIP' + ]; + + foreach ($tags as $tag) { + \App\Models\Tag::create(['name' => $tag]); + } } } diff --git a/resources/views/customer/tagsList.blade.php b/resources/views/customer/tagsList.blade.php new file mode 100644 index 0000000..e7e22a9 --- /dev/null +++ b/resources/views/customer/tagsList.blade.php @@ -0,0 +1,8 @@ +@foreach ($tags as $tag) +
+ + {{ $tag->name }} + +
+@endforeach