Nova Ckeditor
  • 🏁Getting Started
    • Introduction
    • Installation
    • Support Us
    • License
    • Demo
  • 🚀Basic Usage
    • Usage
    • Notes
  • ⚙️Advanced Usage
    • Customize ImageStorage
    • Integrating NovaFileArtisan (Larupload) with Image Resource
    • Customize AudioStorage
    • Customize FileStorage
    • Multiple Toolbars
    • Customize Toolbar Items
    • Media Embed
    • CkEditor Field Options
      • Toolbar
      • Height
      • Limit On Index
      • Content Language
      • Force Paste as Plain Text
      • Alert Before Unsaved Changes
      • Text Part Language
      • General HTML Support
      • Group Items In Overflow Mode
      • Image Browser
      • Audio Browser
      • Video Browser
      • File Browser
      • Snippets
    • Configuration
      • Video Model
      • Image Model
      • Memory
      • Max Image Quality
      • Image Max Width
      • Image Max Height
      • Image Naming Method
      • Image Processing Library
      • Audio Naming Method
      • File Naming Method
      • Toolbars
        • Default Toolbar
        • Toolbar 1
          • Height
          • Items
          • Options
          • Content Lang
          • Force Paste as Plain Text
          • Alert Before Unsaved Changes
          • UI Language
            • UI Language Name
            • UI Language Script
          • Text Part Language
          • General HTML Support
          • Group Items In Overflow Mode
          • Image Browser
          • Audio Browser
          • Video Browser
          • File Browser
          • Insert Image Types
          • Insert Image Size
          • Snippets
  • ⚪Other
    • Migration
    • Credit and Thanks
Powered by GitBook
On this page
Edit on GitHub
  1. Advanced Usage

Integrating NovaFileArtisan (Larupload) with Image Resource

Since v6.1.0

PreviousCustomize ImageStorageNextCustomize AudioStorage

Last updated 1 year ago

To enhance your Nova application's image handling capabilities, you can seamlessly integrate , a feature-rich file uploader package that serves as a wrapper for . Follow these steps to incorporate NovaFileArtisan into your Image Resource:

  1. Install NovaFileArtisan and Larupload packages:

composer require mostafaznv/nova-file-artisan
  1. Modify your images table schema:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;


return new class extends Migration
{
    public function up(): void
    {
        Schema::create('images', function (Blueprint $table) {
            $table->id();
            $table->string('name')->index();
            $table->upload('file');
            $table->timestamps();
        });
    }


    public function down(): void
    {
        Schema::drop('images');
    }
};
  1. Apply the Larupload trait to your Image model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Mostafaznv\Larupload\Enums\LaruploadMediaStyle;
use Mostafaznv\Larupload\Enums\LaruploadNamingMethod;
use Mostafaznv\Larupload\Storage\Attachment;
use Mostafaznv\Larupload\Traits\Larupload;


class Image extends Model
{
    use Larupload;

    protected $fillable = [
        'name', 'file'
    ];

    protected static function booted(): void
    {
        parent::booted();

        self::saving(function ($model) {
            if (!$model->name) {
                $name = $model->file->meta('name');

                $model->name = pathinfo($name, PATHINFO_FILENAME);
            }
        });
    }

    public function attachments(): array
    {
        return [
            Attachment::make('file')
                ->disk('image')
                ->namingMethod(LaruploadNamingMethod::HASH_FILE)
                ->coverStyle('cover', 500, 500, LaruploadMediaStyle::CROP),
        ];
    }
}
  1. Replace ImageUpload with NovaFileArtisan field in your Nova resource:

<?php

namespace App\Nova\Resources;

use App\Nova\Resource;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\Text;
use App\Models\Image as ImageModel;
use Mostafaznv\NovaFileArtisan\Fields\NovaFileArtisan;
use Mostafaznv\NovaFileArtisan\Fields\NovaFileArtisanMeta;


class Image extends Resource
{
    public static string $model = ImageModel::class;
    
    
    public function fields(Request $request): array
    {
        return [
            Text::make(trans('Name'), 'name')
                ->showOnPreview()
                ->help(trans('The file name that should be searched'))
                ->sortable(),

            NovaFileArtisan::make('File', 'file')
                ->showOnPreview()
                ->rules('required', 'mimes:jpg,jpeg,png,gif,webp', 'max:5000')
                ->help(trans(':size Megabyte Max FileSize.', ['size' => 5])),

            ...NovaFileArtisanMeta::make('file')->all(),
        ];
    }
}
  1. You're Done!

These steps empower your Nova application with advanced file handling capabilities using NovaFileArtisan. Feel free to explore additional features and customization options offered by Larupload and NovaFileArtisan.

⚙️
NovaFileArtisan
Larupload