Skip to content

Commit

Permalink
Added contacts page to client resource
Browse files Browse the repository at this point in the history
  • Loading branch information
lukacavic committed Oct 9, 2024
1 parent b2edd2c commit 57306f3
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/Filament/Resources/ClientResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Filament\Resources;

use App\Filament\Resources\ClientResource\Pages;
use App\Filament\Resources\ClientResource\Pages\ClientContacts;
use App\Filament\Resources\ClientResource\Pages\ClientDocuments;
use App\Filament\Resources\ClientResource\Pages\ClientNotes;
use App\Filament\Resources\ClientResource\Pages\ClientOverview;
Expand Down Expand Up @@ -120,6 +121,16 @@ public static function sidebar(Model $record): FilamentPageSidebar
return request()->routeIs(ClientOverview::getRouteName());
}),

PageNavigationItem::make(__('Contacts'))
->icon('heroicon-o-users')
->url(function () use ($record) {
return static::getUrl('contacts', ['record' => $record->id]);
})
->isActiveWhen(function () {
return request()->routeIs(ClientContacts::getRouteName());
}),


PageNavigationItem::make(__('Notes'))
->icon('heroicon-o-pencil-square')
->url(function () use ($record) {
Expand Down Expand Up @@ -149,6 +160,7 @@ public static function getPages(): array
'overview' => ClientOverview::route('/{record}/overview'),
'notes' => ClientNotes::route('/{record}/notes'),
'documents' => ClientDocuments::route('/{record}/documents'),
'contacts' => ClientContacts::route('/{record}/contacts'),
];
}
}
85 changes: 85 additions & 0 deletions app/Filament/Resources/ClientResource/Pages/ClientContacts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace App\Filament\Resources\ClientResource\Pages;

use App\Filament\Resources\ClientResource;
use AymanAlhattami\FilamentPageWithSidebar\Traits\HasPageSidebar;
use Filament\Forms\Form;
use Filament\Resources\Pages\ManageRelatedRecords;
use Filament\Tables\Actions\BulkActionGroup;
use Filament\Tables\Actions\CreateAction;
use Filament\Tables\Actions\DeleteAction;
use Filament\Tables\Actions\DeleteBulkAction;
use Filament\Tables\Actions\EditAction;
use Filament\Tables\Actions\ForceDeleteAction;
use Filament\Tables\Actions\ForceDeleteBulkAction;
use Filament\Tables\Actions\RestoreAction;
use Filament\Tables\Actions\RestoreBulkAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ToggleColumn;
use Filament\Tables\Table;
use Illuminate\Contracts\Support\Htmlable;

class ClientContacts extends ManageRelatedRecords
{
use HasPageSidebar;

protected static string $resource = ClientResource::class;

protected static string $relationship = 'contacts';

public function getTitle(): string|Htmlable
{
return __('Contacts');
}

public function form(Form $form): Form
{
return $form
->schema([

])->columns(1);
}

public function table(Table $table): Table
{
return $table
->recordTitleAttribute('title')
->columns([
TextColumn::make('name')
->label(__('Name')),

TextColumn::make('position')
->label(__('Position')),

ToggleColumn::make('active')
->label(__('Active')),

TextColumn::make('email')
->label(__('Email')),

TextColumn::make('phone')
->label(__('Phone')),

TextColumn::make('last_login_at')
->label(__('Last Login at'))
->dateTime()
])
->headerActions([
CreateAction::make(),
])
->actions([
EditAction::make(),
DeleteAction::make(),
ForceDeleteAction::make(),
RestoreAction::make(),
])
->bulkActions([
BulkActionGroup::make([
DeleteBulkAction::make(),
RestoreBulkAction::make(),
ForceDeleteBulkAction::make(),
]),
]);
}
}
6 changes: 6 additions & 0 deletions app/Models/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\Traits\Organisationable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;

class Client extends BaseModel
Expand All @@ -16,6 +17,11 @@ public function notes(): MorphMany
return $this->morphMany(Note::class, 'related');
}

public function contacts(): HasMany
{
return $this->hasMany(Contact::class, 'client_id');
}

public function documents(): MorphMany
{
return $this->morphMany(Document::class, 'related');
Expand Down
17 changes: 17 additions & 0 deletions app/Models/Contact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Models;

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

class Contact extends BaseModel
{
use HasFactory;

public function client(): BelongsTo
{
return $this->belongsTo(Client::class);
}
}
37 changes: 37 additions & 0 deletions database/migrations/2024_10_09_180357_create_contacts_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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('contacts', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('position')->nullable();
$table->string('email')->nullable();
$table->string('password')->nullable();
$table->boolean('primary_contact')->default(false);
$table->string('phone')->nullable();
$table->unsignedInteger('client_id')->nullable();
$table->boolean('active')->default(false);
$table->timestamp('last_login_at')->nullable();
$table->unsignedInteger('organisation_id');
$table->softDeletes();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('contacts');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<x-filament-panels::page>

</x-filament-panels::page>

0 comments on commit 57306f3

Please sign in to comment.