Has-Images

Has-Images

Has-Images provides a quick and easy way to link images 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-images
php artisan migrate

(Optional) To publish the config & migrations.

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

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 HasImages in Article Model
...
use Pharaonic\Laravel\Images\HasImages;

class Article extends Model
{
    use HasImages;

    // optional
    protected $filesOptions 	= [
        'images'    => [
            'directory'	=> '/articles/images',
            'thumbnail' => [
                'ratio'     => true,
                'width'     => 400,
                'height'    => null
            ]
        ],
    ];
    ...
}

How to use

Uploading & Getting & Deleting files.

$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
echo $article->images[0]->thumbnail->url; 	// Getting image's thumbnail URL if exists

// Create Article
$article = new Article;
...
$article->images = [			// Setting Images List
    $request->file1,
    $request->file2,
    $request->file3
];
$article->save();

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