Laravel-Backpack / CRUD

Build custom admin panels. Fast!
https://backpackforlaravel.com
MIT License
3.11k stars 890 forks source link

Image field and HasTraslation trait issue #5484

Closed miquelangeld closed 4 months ago

miquelangeld commented 6 months ago

Bug report

What I did

When I enable HasTranslation trait, the image fields delete the image path when I save the form even without any change, the translatable field is not the image field. If I remove the trait, then all works fine.

<?php

namespace App\Models;

use Backpack\CRUD\app\Models\Traits\CrudTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\app\Models\Traits\SpatieTranslatable\HasTranslations;

class Feature extends Model
{
    use CrudTrait;
    use HasFactory;
    use HasTranslations;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'description',
        'picto',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'id' => 'integer',
    ];
    protected $translatable = ['name'];
    public function bars()
    {
        return $this->morphedByMany(Bar::class, 'featurable');
    }

    public function restaurants()
    {
        return $this->morphedByMany(Restaurant::class, 'featurable');
    }
}
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\FeatureRequest;
use Backpack\CRUD\app\Http\Controllers\CrudController;
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;

/**
 * Class FeaturesCrudController
 * @package App\Http\Controllers\Admin
 * @property-read \Backpack\CRUD\app\Library\CrudPanel\CrudPanel $crud
 */
class FeatureCrudController extends CrudController
{
    use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
    use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
    use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
    use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
    use \Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation;

    /**
     * Configure the CrudPanel object. Apply settings to all operations.
     *
     * @return void
     */
    public function setup()
    {
        CRUD::setModel(\App\Models\Feature::class);
        CRUD::setRoute(config('backpack.base.route_prefix') . '/feature');
        CRUD::setEntityNameStrings('feature', 'features');
    }

    /**
     * Define what happens when the List operation is loaded.
     *
     * @see  https://backpackforlaravel.com/docs/crud-operation-list-entries
     * @return void
     */
    protected function setupListOperation()
    {
        CRUD::setFromDb(); // set columns from db columns.

        /**
         * Columns can be defined using the fluent syntax:
         * - CRUD::column('price')->type('number');
         */
    }

    /**
     * Define what happens when the Create operation is loaded.
     *
     * @see https://backpackforlaravel.com/docs/crud-operation-create
     * @return void
     */
    protected function setupCreateOperation()
    {
        CRUD::setValidation(FeatureRequest::class);
        CRUD::field([
            'name'  => 'name',
            'label' => 'Name',
            'wrapper' => [
                'class' => 'form-group col-md-3'
            ]]);
        CRUD::field([
            'name'  => 'picto',
            'type'  => 'image',
            'label' => 'Pictogram',
            'withFiles' => true,
            'wrapper' => [
                'class' => 'form-group col-md-3'
            ]]);
            CRUD::field([
            'name'  => 'description',
            'type'  => 'text',
            'label' => 'Imagen selector',
            'wrapper' => [
                'class' => 'form-group col-md-3'
            ]]);

        /**
         * Fields can be defined using the fluent syntax:
         * - CRUD::field('price')->type('number');
         */
    }

    /**
     * Define what happens when the Update operation is loaded.
     *
     * @see https://backpackforlaravel.com/docs/crud-operation-update
     * @return void
     */
    protected function setupUpdateOperation()
    {
        $this->setupCreateOperation();
    }
}

Backpack, Laravel, PHP, DB version

When I run php artisan backpack:version the output is:

PHP VERSION:

PHP 8.3.4 (cli) (built: Mar 16 2024 08:40:08) (NTS) Copyright (c) The PHP Group Zend Engine v4.3.4, Copyright (c) Zend Technologies with Zend OPcache v8.3.4, Copyright (c), by Zend Technologies with Xdebug v3.3.1, Copyright (c) 2002-2023, by Derick Rethans

LARAVEL VERSION:

10.45.1.0

BACKPACK PACKAGE VERSIONS:

backpack/basset: 1.2.4 backpack/crud: 6.6.4 backpack/devtools: 3.0.4 backpack/editable-columns: 3.0.7 backpack/generators: v4.0.3 backpack/pagemanager: 3.3.0 backpack/permissionmanager: 7.2.0 backpack/pro: 2.1.7 backpack/theme-tabler: 1.2.3

pxpm commented 6 months ago

@miquelangeld thanks for the report. 🙏

There is a PR for uploaders that we are testing rn, that fixes a lot of stuff and it may fix this too.

I would rather evaluate this after we finish working on that PR.

It's #5478 in CRUD and https://github.com/Laravel-Backpack/PRO/pull/232 .

I will get back here before merging those PR's to make sure this is covered.

Cheers

miquelangeld commented 6 months ago

Hi @pxpm I updated CRUD from 6.6.4 => 6.7.5 and now seems that all works fine. Tomorrow I will do more tests to confirm. I think the PR that you mentioned it's not merged yet isn't?

miquelangeld commented 4 months ago

Confirmed. It's working