Has-Files

Has-Files

Has-Files provides a quick and easy way to link files with a model.

Moamen Eltouny (Raggi)
PHP Version : >= 7.2 Laravel Version : >= 6.0 License Support
Source Packagist Version Packagist Downloads

Install

Install the latest version using Composer

composer require pharaonic/laravel-has-files
php artisan migrate

(Optional) To publish the config & migrations.

php artisan vendor:publish --tag=laravel-uploader
php artisan vendor:publish --tag=laravel-has-files

Depends on : Laravel Uploader

Usage

Configurations & Options

You can find detailed configurations and options here on : Laravel Uploader

You have to read it clearly if you need a full-access.

Inclusion

Including the Laravel-Has-Files in Model.

// An example
// Using HasFiles in Person Model
...
use Pharaonic\Laravel\Files\HasFiles;

class Person extends Model
{
    use HasFiles;
    
    // if not in defaults in config file
    protected $filesAttributes  = ['passport', 'card'];
    
    // optional
    protected $filesOptions 	= [
        'passport'	=> [
            'private'	=> true,
            // 'visitable'	=> true,
            'directory'	=> '/papers/passports'
        ],

        'card'	=> [
            'directory'	=> '/papers/cards',
            'thumbnail' => [
                'ratio'     => true,
                'width'     => 350,
                'height'    => null
            ]
        ],
    ];
    ...
}

How to use

Uploading & Getting & Deleting files.

// Retrive Person
$person = Person::find(1); 		        // Model
$person->passport = $request->myFile;   // Request Input File + Uploading it
$person->card = $request->myFile2;   
echo $person->passport->url; 	        // Getting passport file URL
echo $person->card->url; 	            // Getting card file URL
echo $person->card->thumbnail->url;     // Getting card thumbnail file URL



// [OR] Create Person
$person = new Person;
...
$person->passport = $request->myFile;
$person->save();
echo $person->passport->url;



// Delete Files
$person->delete(); 				// Delete Person with all related files
// OR
$person->clearFiles();			// Delete all related files
// OR
$person->passport->delete();	// Delete file with thumbnail if exists