Skip to content

Commit

Permalink
Creating Tags for Customers
Browse files Browse the repository at this point in the history
  • Loading branch information
tqt97 committed Oct 15, 2023
1 parent fc26222 commit c226770
Show file tree
Hide file tree
Showing 11 changed files with 256 additions and 1 deletion.
9 changes: 8 additions & 1 deletion app/Filament/Resources/CustomerResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
]);
}

Expand All @@ -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(),
Expand Down
98 changes: 98 additions & 0 deletions app/Filament/Resources/TagResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace App\Filament\Resources;

use App\Filament\Resources\TagResource\Pages;
use App\Filament\Resources\TagResource\RelationManagers;
use App\Models\Tag;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class TagResource extends Resource
{
protected static ?string $model = Tag::class;

protected static ?string $navigationGroup = 'Settings';

public static function form(Form $form): Form
{
return $form
->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'),
];
}
}
12 changes: 12 additions & 0 deletions app/Filament/Resources/TagResource/Pages/CreateTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Filament\Resources\TagResource\Pages;

use App\Filament\Resources\TagResource;
use Filament\Actions;
use Filament\Resources\Pages\CreateRecord;

class CreateTag extends CreateRecord
{
protected static string $resource = TagResource::class;
}
19 changes: 19 additions & 0 deletions app/Filament/Resources/TagResource/Pages/EditTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Filament\Resources\TagResource\Pages;

use App\Filament\Resources\TagResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;

class EditTag extends EditRecord
{
protected static string $resource = TagResource::class;

protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
];
}
}
19 changes: 19 additions & 0 deletions app/Filament/Resources/TagResource/Pages/ListTags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Filament\Resources\TagResource\Pages;

use App\Filament\Resources\TagResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;

class ListTags extends ListRecords
{
protected static string $resource = TagResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}
6 changes: 6 additions & 0 deletions app/Models/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class Customer extends Model
Expand All @@ -24,4 +25,9 @@ public function leadSource(): BelongsTo
{
return $this->belongsTo(LeadSource::class);
}

public function tags(): BelongsToMany
{
return $this->belongsToMany(Tag::class);
}
}
19 changes: 19 additions & 0 deletions app/Models/Tag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Tag extends Model
{
use HasFactory;

protected $fillable = ['name', 'color'];

public function customers(): BelongsToMany
{
return $this->belongsToMany(Customer::class);
}
}
29 changes: 29 additions & 0 deletions database/migrations/2023_10_15_100559_create_tags_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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('tags', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('color')->nullable();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('tags');
}
};
29 changes: 29 additions & 0 deletions database/migrations/2023_10_15_100745_create_customer_tag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use App\Models\Customer;
use App\Models\Tag;
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('customer_tag', function (Blueprint $table) {
$table->foreignIdFor(Customer::class)->constrained();
$table->foreignIdFor(Tag::class)->constrained();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('customer_tag');
}
};
9 changes: 9 additions & 0 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
}
8 changes: 8 additions & 0 deletions resources/views/customer/tagsList.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@foreach ($tags as $tag)
<div class="fi-badge flex items-center justify-center gap-x-1 rounded-md text-xs font-medium ring-1 ring-inset px-1.5 min-w-[theme(spacing.5)] py-0.5 tracking-tight"
style="background: {{ $tag->color }}; display: inline-block;">
<span class="grid">
<span class="truncate">{{ $tag->name }}</span>
</span>
</div>
@endforeach

0 comments on commit c226770

Please sign in to comment.