Crud: Mutator is not fired

Created on 22 Oct 2016  路  6Comments  路  Source: Laravel-Backpack/CRUD

I have this in my CRUD controller

$this->crud->addField([ 
            'name' => 'logo',
            'label' => 'Company Logo',
            'type' => 'upload',
            'upload' => true
        ]);

and this in the model

public function setImageAttribute($value)
    {

        $attribute_name = "image";
        $disk = "uploads";
        $destination_path = "logo";

        $this->uploadFileToDisk($value, $attribute_name, $disk, $destination_path);
    }

When I edit an entry or add one, in the DB, I get something similar to this

/tmp/phppc4Zql

I am not sure the mutator is fired on form submission. Is there possibly anything I am doing wrong?

Most helpful comment

Your field is called logo not image

Thus

setImageAttribute will not fire, it will be setLogoAttribute

All 6 comments

Hmm... If it places some path in the database it means the mutator is being fired.

1) Do you have the "upload" disk setup in your config/filesystems.php?

        'uploads' => [
            'driver' => 'local',
            'root' => public_path('uploads'),
        ],

2) What if you also specify the disk in the field definition, like in the upload field documentation. Does it work then?

[   // Upload
    'name' => 'image',
    'label' => 'Image',
    'type' => 'upload',
    'upload' => true,
    'disk' => 'uploads' // if you store files in the /public folder, please ommit this; if you store them in /storage or S3, please specify it;
],

Your field is called logo not image

Thus

setImageAttribute will not fire, it will be setLogoAttribute

Damn, @OwenMelbz . Twice today my answer was wrong and you came along and stole the show :-) Either you're particularly sharp today or I'm off :-)

Cheers, keep at it! (I still owe you 5 pounds)

i think i have the same issue... i have 5 image fields name as img1, img2, im3 and so on...
i added a mutator setImg5Attribute (i've made tests only on the last image field) with the following data:

$attribute_name = "image";
$disk = "uploads";
$destination_path = "uploads/autoturisme";

but i can't see anything neither in the db neither in the folder /public/uploads/autoturisme

@aureldragut did you include img5 in your fillable property on the model?

That's the first thing i check. There were more mistakes made, one of them being missfilled $attribute_name in the mutator (i left "image" instead of the field name). Now it works smooth, i just have problems with memory (browser crashes when i use bigger images - i may need to reformat my pc, is acting quite strange for a few days)...

Was this page helpful?
0 / 5 - 0 ratings