PHP Classes


Recommend this page to a friend!
  Classes of Moamen Eltouny  >  Laravel File Model  >  >  Download  
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: Laravel File Model
Add support for file properties in Laravel models
Author: By
Last change: Update of
Date: 1 year ago
Size: 4,055 bytes


Class file image Download

<p align="center"><a href="" target="_blank"><img src="" width="470"></a></p>

<p align="center"> <a href="" target="_blank"><img src="" alt="Source"></a> <a href="" target="_blank"><img src="" alt="Packagist Version"></a><br> <a href="" target="_blank"><img src=">=6.0-red.svg?style=flat-square" alt="Laravel"></a> <img src="" alt="Packagist Downloads"> <img src="" alt="Source"> </p>

Laravel files provides a quick and easy way to link files with a model.


Install the latest version using Composer:

$ composer require pharaonic/laravel-has-files

then publish the migration & config files

# if you didn't publish Pharaonic\laravel-uploader before.

$ php artisan vendor:publish --tag=laravel-uploader

$ php artisan vendor:publish --tag=laravel-has-files
$ php artisan migrate


<a name="config"></a>


*	config/Pharaonic/files.php
*	default files fields
*	files 		=> ['image', 'picture', 'cover', 'thumbnail', 'video', 'audio', 'file']

<a name="INC"></a>

Including it in a Model

// An example
// Using HasFiles in Person Model
use Pharaonic\Laravel\Files\HasFiles;
use Pharaonic\Laravel\Helpers\Traits\HasCustomAttributes;

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

<a name="HTU"></a>

How to use

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

// Create Person
$person = new Person;
$person->passport = $request->myFile;
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

<a name="UP"></a>

Uploader Options

$person->passport is retrieving Uploader Object.

That's allow for us use all Pharaonic/laravel-uploader options.

$file = $person->passport;
// Information
echo $file->hash; // File's Hash
echo $file->name; // File's Name
echo $file->path; // File's Path
echo $file->size; // File's Size in Bytes
echo $file->readableSize(); // File's Readable Size B, KB, MB, ...
echo $file->readableSize(false); // File's Readable Size B, KiB, MiB, ...
echo $file->extension; // File's Extension
echo $file->mime; // File's MIME

echo $file->visits; // File's visits (Visitable File)

// Getting URL
echo $file->url; // Getting Uploaded File's URL

// Deleting The File

// Permits (Private File)
$permits = $file->permits; // Getting Permits List
$permitted = $file->isPermitted($user); // Checking if permitted (App\User)

$file->permit($user, '2021-02-01'); // Permitting a user
$file->forbid($user); // Forbidding a user


MIT license

For more information send a message to info at phpclasses dot org.