kakhura/laravel-galleriable

This package is for create galleriable modules

1.0.1 2020-12-05 15:12 UTC

This package is auto-updated.

Last update: 2024-11-05 23:18:19 UTC


README

This package is for create modules, which has media gallery.

Docs

Installation

Add the package in your composer.json by executing the command.

composer require kakhura/laravel-galleriable

For Laravel versions before 5.5 or if not using auto-discovery, register the service provider in config/app.php

'providers' => [
    /*
     * Package Service Providers...
     */
    \Kakhura\Galleriable\GalleriableServiceProvider::class,
],

Configuration

If you want to change default configuration, you must publish default configuration file to your project by running this command in console:

php artisan vendor:publish --tag=kakhura-galleriable-config

This command will copy file [/vendor/kakhura/laravel-galleriable/config/kakhura.galleriable.php] to [/config/kakhura.galleriable.php]

Default kakhura.galleriable.php looks like:

return [
    /**
     * Use soft deletes into models and database.
     */
    'use_soft_deletes' => true,
];

This command will copy file [/vendor/kakhura/laravel-galleriable/resources/views] to [/resources/views/vendor/admin/galleriable]

Migrations

After publish Configuration, you must publish migrations, by running this command in console:

php artisan vendor:publish --tag=kakhura-galleriable-migrations

This command will copy file [/vendor/kakhura/laravel-galleriable/database/migrations] to [/database/migrations]

After publish Migrations, you must add HasGallery trait in your model which you want to has gallery:

use Kakhura\Galleriable\Traits\Models\HasGallery;

class Post extends Model
{
    use HasGallery;
}

You must sync Gallery in all your model create functionality like this:

use Models\Post;

class PostService extends Service
{
    public fucntion create(array $data) 
    {
        ...
        $post = Post::create($data);
        $images = [];
        foreach (Arr::get($data, 'images') as $key => $image) {
            $file = Helper::uploadFile($data, $fileType);
            $images[] = [
                'image_id' => $file->id,
                'sort_index' => $key,
            ];
        }
        $post->syncGallery($images);
        ...
    }
}

Enjoy.