PHP Classes

Laravel File Model: Add support for file properties in Laravel models

Recommend this page to a friend!
     
  Info   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 47 All time: 10,760 This week: 455Up
Version License PHP version Categories
laravel-files 1.0.0MIT/X Consortium ...7PHP 5, Files and Folders, Libraries, D...
Description 

Author

This package can add support for file properties in Laravel models.

It provides a trait that can be used in model classes used in Laravel based applications, so model attributes can be used to store and retrieve data from files.

The package allows model classes to store data from files uploaded to Web server, serve stored data as files for download and delete model data.

Innovation Award
PHP Programming Innovation award nominee
August 2020
Number 3
Usually model classes are used to store and retrieve application objects in database tables.

However, for object variables that can contain data that maybe large, it may be better to use files to store the values of those values in files.

This way, the database table records do not become too large, thus avoiding to slow down the database access.

This package implements a trait to use with model classes based on the Laravel framework to make some of the model class objects be mapped to files.

Manuel Lemos
Picture of Moamen Eltouny
  Performance   Level  
Name: Moamen Eltouny <contact>
Classes: 38 packages by
Country: Egypt Egypt
Age: 31
All time rank: 257626 in Egypt Egypt
Week rank: 206 Up3 in Egypt Egypt Up
Innovation award
Innovation award
Nominee: 22x

Details

<p align="center"><a href="https://pharaonic.io" target="_blank"><img src="https://raw.githubusercontent.com/Pharaonic/logos/main/has-files.jpg" width="470"></a></p>

<p align="center"> <a href="https://github.com/Pharaonic/laravel-has-files" target="_blank"><img src="http://img.shields.io/badge/source-pharaonic/laravel--has--files-blue.svg?style=flat-square" alt="Source"></a> <a href="https://packagist.org/packages/pharaonic/laravel-has-files" target="_blank"><img src="https://img.shields.io/packagist/v/pharaonic/laravel-has-files?style=flat-square" alt="Packagist Version"></a><br> <a href="https://laravel.com" target="_blank"><img src="https://img.shields.io/badge/Laravel->=6.0-red.svg?style=flat-square" alt="Laravel"></a> <img src="https://img.shields.io/packagist/dt/pharaonic/laravel-has-files?style=flat-square" alt="Packagist Downloads"> <img src="http://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square" alt="Source"> </p>

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

Install

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

Usage

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

Configuration

/
*	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;
$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

<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
$file->delete();


// 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

License

MIT license


  Files folder image Files (8)  
File Role Description
Files folder imagesrc (3 files, 2 directories)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (8)  /  src  
File Role Description
Files folder imageconfig (1 file)
Files folder imagedatabase (1 directory)
  Plain text file File.php Class Class source
  Plain text file FilesServiceProvider.php Class Class source
  Plain text file HasFiles.php Class Class source

  Files folder image Files (8)  /  src  /  config  
File Role Description
  Accessible without login Plain text file files.php Aux. Auxiliary script

  Files folder image Files (8)  /  src  /  database  
File Role Description
Files folder imagemigrations (1 file)

  Files folder image Files (8)  /  src  /  database  /  migrations  
File Role Description
  Plain text file files.stub Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:47
This week:0
All time:10,760
This week:455Up