kongulov / nova-tab-translatable

This package contains a NovaTabTranslatable class you can use to make any Nova field type translatable with tabs.
MIT License
79 stars 17 forks source link

File field doesn't support storeAs() and other related methods #42

Open Rinze-Smits opened 8 months ago

Rinze-Smits commented 8 months ago

I have a File field on my resource which uses storeAs() to set a custom file name. When uploading a file it doesn't use my storeAs method and instead generates a random string to use as name.

For example:

Fields\File::make('FileField')->storeAs(function (NovaRequest $request) {
    return now()->format('Y-m-d h-i-s ') . $request->file_field->getClientOriginalName();
})

results in a file named ldQsVjMAkFRnuZYZcvkDJ4NFFiWGCl2ji6CBjlTJ.docx when uploading a docx file.

The custom store method set in createTranslatedField doesn't seem to look at any defined storeAsCallback, nor calls the original fields' store method. Also other parts of the File field or not implemented, e.g. storeOriginalName() and storeSize().

kongulov commented 8 months ago

@Rinze-Smits Thank you very much for using the package.

Could you write what version of the packages you are using and post an example of use so that I can repeat the same problem myself and solve it

Rinze-Smits commented 8 months ago

@kongulov Thank you so much for making the package!

The versions I use are all the latest as of now.

kongulov/nova-tab-translatable 2.1.3 spatie/laravel-translatable 6.6.1 laravel/nova 4.32.15 laravel/framework v10.46.0

Model:

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;

class TestModel extends Model
{
    use HasFactory;
    use HasTranslations;

    public $translatable = ['test_file', 'test_name', 'test_size', 'working_file', 'working_name', 'working_size'];
}

Migration:

<?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('test_models', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->json('test_file')->nullable();
            $table->json('test_name')->nullable();
            $table->json('test_size')->nullable();
            $table->json('working_file')->nullable();
            $table->json('working_name')->nullable();
            $table->json('working_size')->nullable();
        });

    }
    public function down(): void{Schema::dropIfExists('test_models');}
};

Resource:

<?php
namespace App\Nova;

use Kongulov\NovaTabTranslatable\NovaTabTranslatable;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Http\Requests\NovaRequest;

class TestModel extends Resource
{
    public static $model = \App\Models\TestModel::class;
    public static $title = 'id';
    public static $search = ['id',];
    public function fields(NovaRequest $request)
    {
        return [
            ID::make()->sortable(),
            NovaTabTranslatable::make([
                \Laravel\Nova\Fields\File::make('test_file')->storeAs(function (NovaRequest $request) {
                    return now()->format('Ymdhis-') . $request->test_file->getClientOriginalName();
                })->storeOriginalName('test_name')->storeSize('test_size'),
            ]),
            \Laravel\Nova\Fields\File::make('working_file')->storeAs(function (NovaRequest $request) {
                return now()->format('Ymdhis-') . $request->working_file->getClientOriginalName();
            })->storeOriginalName('working_name')->storeSize('working_size'),
        ];
    }
    public function cards(NovaRequest $request){return [];}
    public function filters(NovaRequest $request){return [];}
    public function lenses(NovaRequest $request){return [];}
    public function actions(NovaRequest $request){return [];}
}

After creating a record with a file and going to the detail page there is a download link with the file. Clicking it shows the browser download dialog with download.htm and a message saying there is no file. The file outside the NovaTabTranslatable can be saved with the custom name and storing original name and size in separate columns.