PHP Classes


Recommend this page to a friend!
  Classes of Moamen Eltouny   Laravel Image Storage   Download  
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: Laravel Image Storage
Store and retrieve images in Laravel models
Author: By
Last change:
Date: 3 years ago
Size: 3,702 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 images provides a quick and easy way to link images with a model.


Install the latest version using Composer:

$ composer require pharaonic/laravel-has-images

then publish the migration & config images

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

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

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


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

Including it in a Model

// An example
// Using HasImages in Article Model
use Pharaonic\Laravel\Images\HasImages;
use Pharaonic\Laravel\Helpers\Traits\HasCustomAttributes;

class Article extends Model
    use HasCustomAttributes, HasImages;

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

How to use

$article = Article::find(1); 					// Model
$images = $article->images // Article Images
$article->addImage($request->fileName);	// Setting New Single Image

echo $article->images[0]->url; 	        // Getting image URL

// Create Article
$article = new Article;
$article->images = [							// Setting Images List

echo $article->images[2]->url;

// Delete Images
$article->delete(); 				// Delete Article with all related images
// OR
$article->clearImages();			// Delete all related images
// OR
$article->images[2]->delete();	// Delete image with Index

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

Uploader Options

$article->images[index number] is retrieving Uploader Object.

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

$image = $article->images[0];
// Information
echo $image->hash; // Image's Hash
echo $image->name; // Image's Name
echo $image->path; // Image's Path
echo $image->size; // Image's Size in Bytes
echo $image->readableSize(); // Image's Readable Size B, KB, MB, ...
echo $image->readableSize(false); // Image's Readable Size B, KiB, MiB, ...
echo $image->extension; // Image's Extension
echo $image->mime; // Image's MIME

echo $image->visits; // Image's visits (Visitable Image)

// Getting URL
echo $image->url; // Getting Uploaded Image's URL

// Deleting The Image

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

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


MIT license