Skip to content

Commit

Permalink
Creating Lead Sources Resource
Browse files Browse the repository at this point in the history
  • Loading branch information
tqt97 committed Oct 15, 2023
1 parent 3784a99 commit fc26222
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/Filament/Resources/CustomerResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public static function form(Form $form): Form
Forms\Components\Textarea::make('description')
->maxLength(65535)
->columnSpanFull(),
Forms\Components\Select::make('lead_source_id')
->relationship('leadSource', 'name'),
]);
}

Expand All @@ -52,6 +54,7 @@ public static function table(Table $table): Table
->searchable(),
Tables\Columns\TextColumn::make('phone_number')
->searchable(),
Tables\Columns\TextColumn::make('leadSource.name'),
Tables\Columns\TextColumn::make('created_at')
->dateTime()
->sortable()
Expand Down
96 changes: 96 additions & 0 deletions app/Filament/Resources/LeadSourceResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace App\Filament\Resources;

use App\Filament\Resources\LeadSourceResource\Pages;
use App\Filament\Resources\LeadSourceResource\RelationManagers;
use App\Models\LeadSource;
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 LeadSourceResource extends Resource
{
protected static ?string $model = LeadSource::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(),
Tables\Actions\DeleteAction::make()
->action(function ($data, $record) {
if ($record->customers()->count() > 0) {
Notification::make()
->danger()
->title('Lead Source is in use')
->body('Lead Source is in use by customers.')
->send();

return;
}

Notification::make()
->success()
->title('Lead Source deleted')
->body('Lead Source 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\ListLeadSources::route('/'),
'create' => Pages\CreateLeadSource::route('/create'),
'edit' => Pages\EditLeadSource::route('/{record}/edit'),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Filament\Resources\LeadSourceResource\Pages;

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

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

namespace App\Filament\Resources\LeadSourceResource\Pages;

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

class EditLeadSource extends EditRecord
{
protected static string $resource = LeadSourceResource::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\LeadSourceResource\Pages;

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

class ListLeadSources extends ListRecords
{
protected static string $resource = LeadSourceResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}
7 changes: 7 additions & 0 deletions app/Models/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

class Customer extends Model
Expand All @@ -16,5 +17,11 @@ class Customer extends Model
'email',
'phone_number',
'description',
'lead_source_id'
];

public function leadSource(): BelongsTo
{
return $this->belongsTo(LeadSource::class);
}
}
15 changes: 15 additions & 0 deletions app/Models/LeadSource.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 LeadSource extends Model
{
use HasFactory;

protected $fillable = [
'name',
];
}
7 changes: 7 additions & 0 deletions app/Providers/Filament/AdminPanelProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Illuminate\Session\Middleware\AuthenticateSession;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\View\Middleware\ShareErrorsFromSession;
use Filament\Navigation\NavigationGroup;

class AdminPanelProvider extends PanelProvider
{
Expand Down Expand Up @@ -51,6 +52,12 @@ public function panel(Panel $panel): Panel
DisableBladeIconComponents::class,
DispatchServingFilamentEvent::class,
])
->navigationGroups([
NavigationGroup::make()
->label('Settings')
->icon('heroicon-o-cog-6-tooth')
->collapsed(),
])
->authMiddleware([
Authenticate::class,
]);
Expand Down
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('lead_sources', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}

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

use App\Models\LeadSource;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Expand All @@ -18,6 +19,7 @@ public function up(): void
$table->string('email')->nullable();
$table->string('phone_number')->nullable();
$table->text('description')->nullable();
$table->foreignIdFor(LeadSource::class)->nullable()->constrained();
$table->timestamps();
$table->softDeletes();

Expand Down
15 changes: 15 additions & 0 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ public function run(): void
'password' => Hash::make('12341234'),
]);



$leadSources = [
'Website',
'Online AD',
'Twitter',
'LinkedIn',
'Webinar',
'Trade Show',
'Referral',
];
foreach ($leadSources as $leadSource) {
\App\Models\LeadSource::create(['name' => $leadSource]);
}

\App\Models\Customer::factory()
->count(10)
->create();
Expand Down

0 comments on commit fc26222

Please sign in to comment.