Skip to content

Commit

Permalink
Custom Fields for Customers
Browse files Browse the repository at this point in the history
  • Loading branch information
tqt97 committed Oct 15, 2023
1 parent c18cef8 commit db6e66b
Show file tree
Hide file tree
Showing 11 changed files with 284 additions and 2 deletions.
75 changes: 75 additions & 0 deletions app/Filament/Resources/CustomFieldResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace App\Filament\Resources;

use App\Filament\Resources\CustomFieldResource\Pages;
use App\Filament\Resources\CustomFieldResource\RelationManagers;
use App\Models\CustomField;
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 CustomFieldResource extends Resource
{
protected static ?string $model = CustomField::class;

protected static ?string $navigationGroup = 'Settings';

public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->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(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}

public static function getRelations(): array
{
return [
//
];
}

public static function getPages(): array
{
return [
'index' => Pages\ListCustomFields::route('/'),
'create' => Pages\CreateCustomField::route('/create'),
'edit' => Pages\EditCustomField::route('/{record}/edit'),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Filament\Resources\CustomFieldResource\Pages;

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

class CreateCustomField extends CreateRecord
{
protected static string $resource = CustomFieldResource::class;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Filament\Resources\CustomFieldResource\Pages;

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

class EditCustomField extends EditRecord
{
protected static string $resource = CustomFieldResource::class;

protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Filament\Resources\CustomFieldResource\Pages;

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

class ListCustomFields extends ListRecords
{
protected static string $resource = CustomFieldResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}
44 changes: 42 additions & 2 deletions app/Filament/Resources/CustomerResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

use App\Filament\Resources\CustomerResource\Pages;
use App\Filament\Resources\CustomerResource\RelationManagers;
use App\Models\CustomField;
use App\Models\Customer;
use App\Models\PipelineStage;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Infolists\Components\RepeatableEntry;
use Filament\Infolists\Components\Section;
use Filament\Infolists\Components\TextEntry;
Expand Down Expand Up @@ -78,7 +80,34 @@ public static function form(Form $form): Form
Forms\Components\Textarea::make('comments'),
])
->columns()
])
]),
Forms\Components\Section::make('Additional fields')
->schema([
Forms\Components\Repeater::make('fields')
->hiddenLabel()
->relationship('customFields')
->schema([
Forms\Components\Select::make('custom_field_id')
->label('Field Type')
->options(CustomField::pluck('name', 'id')->toArray())
// We will disable already selected fields
->disableOptionWhen(function ($value, $state, Get $get) {
return collect($get('../*.custom_field_id'))
->reject(fn ($id) => $id === $state)
->filter()
->contains($value);
})
->required()
// Adds search bar to select
->searchable()
// Live is required to make sure that the options are updated
->live(),
Forms\Components\TextInput::make('value')
->required()
])
->addActionLabel('Add another Field')
->columns(),
]),

]);
}
Expand Down Expand Up @@ -197,6 +226,17 @@ public static function infoList(Infolist $infolist): Infolist
TextEntry::make('pipelineStage.name'),
])
->columns(),
Section::make('Additional fields')
->hidden(fn ($record) => $record->customFields->isEmpty())
->schema(
// We are looping within our relationship, then creating a TextEntry for each Custom Field
fn ($record) => $record->customFields->map(function ($customField) {
return TextEntry::make($customField->customField->name)
->label($customField->customField->name)
->default($customField->value);
})->toArray()
)
->columns(),
Section::make('Documents')
// This will hide the section if there are no documents
->hidden(fn ($record) => $record->documents->isEmpty())
Expand All @@ -208,7 +248,6 @@ public static function infoList(Infolist $infolist): Infolist
->label('Document')
// This will rename the column to "Download Document" (otherwise, it's just the file name)
->formatStateUsing(fn () => "Download Document")
// URL to be used for the download (link), and the second parameter is for the new tab
->url(fn ($record) => Storage::url($record->file_path), true)
// This will make the link look like a "badge" (blue)
->badge()
Expand All @@ -217,6 +256,7 @@ public static function infoList(Infolist $infolist): Infolist
])
->columns()
]),

Section::make('Pipeline Stage History and Notes')
->schema([
ViewEntry::make('pipelineStageLogs')
Expand Down
15 changes: 15 additions & 0 deletions app/Models/CustomField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Models;

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

class CustomField extends Model
{
use HasFactory;

protected $fillable = [
'name'
];
}
25 changes: 25 additions & 0 deletions app/Models/CustomFieldCustomer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Models;

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

// pivot Model
class CustomFieldCustomer extends Model
{
use HasFactory;

protected $guarded = ['id'];

public function customer(): BelongsTo
{
return $this->belongsTo(Customer::class);
}

public function customField(): BelongsTo
{
return $this->belongsTo(CustomField::class);
}
}
5 changes: 5 additions & 0 deletions app/Models/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ public function documents(): HasMany
{
return $this->hasMany(Document::class);
}

public function customFields(): HasMany
{
return $this->hasMany(CustomFieldCustomer::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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('custom_fields', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('custom_fields');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use App\Models\CustomField;
use App\Models\Customer;
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('custom_field_customers', function (Blueprint $table) {
$table->id();
$table->foreignIdFor(Customer::class)->constrained();
$table->foreignIdFor(CustomField::class)->constrained();
$table->string('value');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('custom_field_customers');
}
};
12 changes: 12 additions & 0 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Database\Seeders;

// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use App\Models\CustomField;
use App\Models\Customer;
use App\Models\LeadSource;
use App\Models\PipelineStage;
Expand Down Expand Up @@ -80,5 +81,16 @@ public function run(): void
Customer::factory()->count(10)->create([
'pipeline_stage_id' => $defaultPipelineStage,
]);

$customFields = [
'Birth Date',
'Company',
'Job Title',
'Family Members',
];

foreach ($customFields as $customField) {
CustomField::create(['name' => $customField]);
}
}
}

0 comments on commit db6e66b

Please sign in to comment.