Skip to content

Commit

Permalink
Customer Documents: Upload/Download
Browse files Browse the repository at this point in the history
  • Loading branch information
tqt97 committed Oct 15, 2023
1 parent 59beb89 commit c18cef8
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 25 deletions.
96 changes: 71 additions & 25 deletions app/Filament/Resources/CustomerResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
use App\Models\PipelineStage;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Infolists\Components\RepeatableEntry;
use Filament\Infolists\Components\Section;
use Filament\Infolists\Components\TextEntry;
use Filament\Infolists\Components\ViewEntry;
use Filament\Infolists\Infolist;
use Filament\Notifications\Notification;
use Filament\Resources\Resource;
use Filament\Support\Colors\Color;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Facades\Storage;

class CustomerResource extends Resource
{
Expand All @@ -29,31 +32,54 @@ public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('first_name')
->maxLength(255),
Forms\Components\TextInput::make('last_name')
->maxLength(255),
Forms\Components\TextInput::make('email')
->email()
->maxLength(255),
Forms\Components\TextInput::make('phone_number')
->maxLength(255),
Forms\Components\Textarea::make('description')
->maxLength(65535)
->columnSpanFull(),
Forms\Components\Select::make('lead_source_id')
->relationship('leadSource', 'name'),
Forms\Components\Select::make('tags')
->relationship('tags', 'name')
->preload()
->multiple(),
Forms\Components\Select::make('pipeline_stage_id')
->relationship('pipelineStage', 'name', function ($query) {
// It is important to order by position to display the correct order
$query->orderBy('position', 'asc');
})
// We are setting the default value to the default Pipeline Stage
->default(PipelineStage::where('is_default', true)->first()?->id),

Forms\Components\Section::make('Customer Details')
->schema([
Forms\Components\TextInput::make('first_name')
->maxLength(255),
Forms\Components\TextInput::make('last_name')
->maxLength(255),
Forms\Components\TextInput::make('email')
->email()
->maxLength(255),
Forms\Components\TextInput::make('phone_number')
->maxLength(255),
Forms\Components\Textarea::make('description')
->maxLength(65535)
->columnSpanFull(),
])
->columns(),
Forms\Components\Section::make('Lead Details')
->schema([
Forms\Components\Select::make('lead_source_id')
->relationship('leadSource', 'name'),
Forms\Components\Select::make('tags')
->relationship('tags', 'name')
->multiple(),
Forms\Components\Select::make('pipeline_stage_id')
->relationship('pipelineStage', 'name', function ($query) {
$query->orderBy('position', 'asc');
})
->default(PipelineStage::where('is_default', true)->first()?->id)
])
->columns(3),
Forms\Components\Section::make('Documents')
// This will make the section visible only on the edit page
->visibleOn('edit')
->schema([
Forms\Components\Repeater::make('documents')
->relationship('documents')
->hiddenLabel()
->reorderable(false)
->addActionLabel('Add Document')
->schema([
Forms\Components\FileUpload::make('file_path')
->required(),
Forms\Components\Textarea::make('comments'),
])
->columns()
])

]);
}

Expand Down Expand Up @@ -171,6 +197,26 @@ public static function infoList(Infolist $infolist): Infolist
TextEntry::make('pipelineStage.name'),
])
->columns(),
Section::make('Documents')
// This will hide the section if there are no documents
->hidden(fn ($record) => $record->documents->isEmpty())
->schema([
RepeatableEntry::make('documents')
->hiddenLabel()
->schema([
TextEntry::make('file_path')
->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()
->color(Color::Blue),
TextEntry::make('comments'),
])
->columns()
]),
Section::make('Pipeline Stage History and Notes')
->schema([
ViewEntry::make('pipelineStageLogs')
Expand Down
6 changes: 6 additions & 0 deletions app/Models/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Models\Document;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand Down Expand Up @@ -52,4 +53,9 @@ public static function booted(): void
]);
});
}

public function documents(): HasMany
{
return $this->hasMany(Document::class);
}
}
32 changes: 32 additions & 0 deletions app/Models/Document.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Models;

use App\Models\Customer;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\Storage;

class Document extends Model
{
use HasFactory;

protected $fillable = [
'customer_id',
'file_path',
'comments'
];

protected static function booted(): void
{
self::deleting(function (Document $customerDocument) {
Storage::disk('public')->delete($customerDocument->file_path);
});
}

public function customer(): BelongsTo
{
return $this->belongsTo(Customer::class);
}
}
31 changes: 31 additions & 0 deletions database/migrations/2023_10_15_105238_create_documents_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

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('documents', function (Blueprint $table) {
$table->id();
$table->foreignIdFor(Customer::class)->constrained();
$table->string('file_path');
$table->text('comments')->nullable();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('documents');
}
};

0 comments on commit c18cef8

Please sign in to comment.