Git Product home page Git Product logo

nova-image-gallery-field's Introduction

Laravel Nova Image Gallery Field

A custom Nova field that allows multiple image upload with sorting

Stable Version License PHP Version Require

Features

  • For Laravel Nova ^4.0
  • Multiple image upload into a spatie/laravel-medialibrary collection.
  • Image sorting
  • Custom Image Validation
  • Drag & Drop
  • Dark Mode

Installation

composer require ardenthq/nova-image-gallery-field

Requirements

Use

Note This package reuses part of the logic used on the Laravel\Nova\Fields\Trix Field to store and handle files. This means we need to follow some steps that are mentioned on the Laravel Nova docs related to adding the migrations and pruning the files.

  1. Define two database tables to store pending and persisted Trix uploads. To do so, create a migration with the following table definitions:
Schema::create('nova_pending_trix_attachments', function (Blueprint $table) {
    $table->increments('id');
    $table->string('draft_id')->index();
    $table->string('attachment');
    $table->string('disk');
    $table->timestamps();
});

Schema::create('nova_trix_attachments', function (Blueprint $table) {
    $table->increments('id');
    $table->string('attachable_type');
    $table->unsignedInteger('attachable_id');
    $table->string('attachment');
    $table->string('disk');
    $table->string('url')->index();
    $table->timestamps();

    $table->index(['attachable_type', 'attachable_id']);
});
  1. In your app/Console/Kernel.php file, you should register a daily job to prune any stale attachments from the pending attachments table and storage. Laravel Nova provides the job implementation needed to accomplish this:
use Laravel\Nova\Trix\PruneStaleAttachments;

$schedule->call(function () {
    (new PruneStaleAttachments)();
})->daily();
  1. Add the ImageGalleryField field to your Nova Resource.

  2. Consider that the images will be stored in a Spatie\MediaLibrary\MediaCollections\Models\Media collection according to the name passed as the first parameter on the field make method or the second parameter if set. (Dont forget to register a media collection in your model)

  3. Use the rules() method to define the rules used for every single image.

  4. Use the rulesMessages() method to define custom validation messages for the image rules.

  5. Use the help() method if you want to place "help" text inside the Drag & Drop area.

Example

<?php
namespace App\Nova;

use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Resource;
use Ardenthq\ImageGalleryField\ImageGalleryField;

final class ResourceName extends Resource
{
    // ....
    public function fields(NovaRequest $request)
    {
        return [
            // ....
            ImageGalleryField::make('Images')
                ->rules('mimes:jpeg,png,jpg,gif', 'dimensions:min_width=150,min_height=150', 'max:5000')
                ->rulesMessages([
                    'mimes'      => 'You must use a valid jpeg, png, jpg or gif image.',
                    'max'        => 'The image must be less than 5MB.',
                    'dimensions' => 'The image must be at least 150px wide and 150px tall.',
                ])
                ->help('Min size 150 x 150. Max filesize 5MB.'),
                // ...
        ];
    }
    // ...
}

Development

  1. Run yarn nova:install and yarn install to install all the necessary dependencies for compiling the view components.
  2. Run yarn run dev (or yarn run watch) while making changes to the components in your local environment.
  3. If you change the vue components, ensure to compile for production before making a PR.

Compile for production

  1. Run yarn nova:install and yarn install to install all the necessary dependencies for compiling the view components.
  2. Run yarn run production.

Analyze the code with phpstan

composer analyse

Refactor the code with php rector

composer refactor

Format the code with php-cs-fixer

composer format

Run tests

composer test

Security

If you discover a security vulnerability within this package, please send an e-mail to [email protected]. All security vulnerabilities will be promptly addressed.

Credits

This project exists thanks to all the people who contribute.

License

MIT © Ardent

nova-image-gallery-field's People

Contributors

alfonsobries avatar itsanametoo avatar elsayed85 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.