Skip to main content

Customer Documents: Upload/Download

Premium
6 min read

Our Customer needs some documents to be uploaded. These can range from simple information sheets to signed PDF documents. We need to be able to upload them and add notes to them, just like this:

In this lesson, we will do the following:

  • Create Documents database table, model
  • Add Documents to the Customer form - only for edit page
    • As a bonus, we will clean up the form a bit
  • Add Documents to the Customer view page as downloadable links

Creating Database Table and Model

Our Documents will have the following fields:

  • id
  • customer_id
  • file_path
  • comments (nullable text)

Let's start with the migration:

Migration

use App\Models\Customer;
 
// ...
 
Schema::create('documents', function (Blueprint $table) {
$table->id();
$table->foreignIdFor(Customer::class)->constrained();
$table->string('file_path');
$table->text('comments')->nullable();
$table->timestamps();
});

Then, we will create the model:

app/Models/Document.php

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Storage;
 
class Document extends Model
{
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);
}
}

Quick note: Look at our deleting observer - we delete the file from the storage when the document is deleted. This is an excellent practice to follow.

And lastly, we need to tie our Customer model to the Document model:

// ...
 
public function documents(): HasMany
{
return $this->hasMany(Document::class);
}

Adding Documents to the Customer Form

Adding Documents to the Customer form is...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 01 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

MR
Martin Radu ✓ Link copied!

before the facelift of app/Filament/Resources/CustomerResource.php you need to php artisan migrate the relationship is ->relationship('customerDocuments') (not ->relationship('documents') )

M
Modestas ✓ Link copied!

Updated the relationship name. It slipped in editing proccess and it should be documents everywhere.

MR
Martin Radu ✓ Link copied!

i get an error in the pipeline-stage-history-list.blade.php foreach() argument must be of type array|object, null given  

M
Modestas ✓ Link copied!

Could you add some code example? There might be something wrong with the relationship or data loading

MR
Martin Radu ✓ Link copied!

adding an if before the foreach solves the problem in the resources/views/infolists/components/pipeline-stage-history-list.blade.php

@if(!empty($getState()))

M
Modestas ✓ Link copied!

In that case, your observer has failed to create a fresh state OR you forgot to freshly migrate the database.

By default, it should have a state as soon as a customer is created by using an observer

MR
Martin Radu ✓ Link copied!

i can see the documents section but i cann't see any document and i uploaded 2 files and i can see them if i edit the customer

M
Modestas ✓ Link copied!

There might be issue with your relationship names. Please change all of them to documents and it should work. If it does not - would be great to see some code examples.

CP
Claudio Pereira ✓ Link copied!

Modestas, how about use RelationManager in place of Repeater to deal with documents? Then we can add tags, use default table search, actions and so on. How this can be done with infolists?

M
Modestas ✓ Link copied!

Personally, I would not use relation manager here. But if you do want to use it - infolist should work as is. it's just a relationship after all, and we are doing cuatom data display (from a relationship)

S
Sjoerd24 ✓ Link copied!

love the simplicity of how documents are added! Looks really clean. a few questions:

  1. I dont see any checks for filetypes, isn't that dangerous if someone uploades a php file? or is laravel /filament doing some invisible checks?
  2. more general i see that a lot of times you use the filament function to change values before they are submitted. Personally i use observers for that, are there any advantages for one over the other? My project is pure filament so far, so maybe I should use that but I like the clearness of observers.
  3. My own plan is (because i need the ability to attach documents everywhere) to make a trait out of it and the documents table polymorphic. Do you have any resources/courses that would help me with that?

Thanks again for everything and I have to say (having two small kids) that I love this text format for learning. It is perfect!

M
Modestas ✓ Link copied!

Hi, first of all - thank you for the kind works!

Now onto your questions:

  1. We did not add any document validation intentionally as we don't know what documents people might want. For sure there should be some!
  2. There is no difference I would say. We just use filament functions to handle these things as they are easier to understand for a big audience. And honestly, less explaining on why there is an observer or something else :)
  3. For that - use spatie media library - it does what you need!
S
Sjoerd24 ✓ Link copied!

thanks for the fast reply! Quick follow up question, I am indeed considering using spatie media library but I dont need all those extra functions and need support for multiple different filetypes and I dont know if that would work.. I read the docs but couldnt find anything about enabling different file types.

Do you have any (filament) example projects with spatiemedia library planned?

M
Modestas ✓ Link copied!

With spatie media library you can upload whatever you want. I've used it in many projects to upload documents, invoices and so on!

As for example, not sure right now (replying from mobile :) ) but we should have some examples of media upload overall

S
Sjoerd24 ✓ Link copied!

Thanks Modestas! Thats great to hear, will try it out. :)

B
Blue_Moonie ✓ Link copied!

If your uploaded files are not found, you may not have a symbolic link from your public folder to your storage folder. You can run the following to fix it:

php artisan storage:link

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.