Has-Files is a package that provides a quick and easy way to link files with a model for Laravel.
This package depends on Uploader.
Install the latest version using Composer.
1composer require pharaonic/laravel-has-files2php artisan migrate
Files uploading is gonna be easiest thing you will think about, so you will be able to store, retrieve, update and delete files so easily.
You have to include HasFiles trait into your model first.
Optional : You can customize the uploading options through Uploader.
1namespace App\Models; 2 3use Illuminate\Database\Eloquent\Model; 4use Pharaonic\Laravel\Files\HasFiles; 5 6class Person extends Model 7{ 8 use HasFiles; 9 10 protected $filesAttributes = ['passport']; 11 12 protected $filesOptions = [
13 'passport' => [14 'directory' => '/papers/passports',15 'thumbnail' => [16 'ratio' => true,17 'width' => 350,18 'height' => null19 ]20 ],21 ];22}
This is how you can store files into your model, you can do that on create or replace the file with a new one.
1// On Create 2$person = Person::create([3 'passport' => $request->passport4]);5 6// Replace File 7$person->passport = $request->passport;8$person->save();
You can retrieve the file easily or it's thumbnail too.
1echo $person->passport->url;2echo $person->passport->thumbnail->url;
You can delete a single file or all files or the model with the files too.
1// Delete Single File 2$person->passport->delete();3 4// Delete All Files 5$person->clearFiles();6 7// Delete Model With It's Files too 8$person->delete();
You can publish the migrations.
1php artisan vendor:publish --tag=laravel-has-files