I have a laravel app that uses backpack. On my user form i have two image fields, the image should be uploaded to a folder and have the name saved on the database. However the mutator that i added on the user model don't seem to fire at all.
The newly created user is saved on the database but the two longtext fields (used for the image name) are saved as null.
I have the same mechanism on another model and here it works fine. I tried seeing if the field names are not coherent and if the disk is declared properly but it all seems good
This is where i declare the fields in the usercrudcontroller:
$this->crud->AddField([ // image
'name' => "imglogo",
'label' => "Logo",
'type' => 'image',
'upload' => true,
'disk' => 'public'
],'imglogo');
$this->crud->AddField([ // image
'name' => "imgheader",
'label' => "Carta Intestata",
'type' => 'image',
'upload' => true,
'disk' => 'public'
],'imgheader');
And these are the mutators that i have in the Backpackuser model:
public function setImglogoAttribute($value)
{
dd($value);
$attribute_name = "imglogo";
$disk = "uploads";
$destination_path = "loghi";
// if the image was erased
if ($value==null) {
// delete the image from disk
\Storage::disk($disk)->delete($this->{$attribute_name});
// set null in the database column
$this->attributes[$attribute_name] = null;
}
// if a base64 was sent, store it in the db
if (starts_with($value, 'data:image'))
{
// 0. Make the image
$image = \Image::make($value)->encode('jpg', 90);
// 1. Generate a filename.
$filename = md5($value.time()).'.jpg';
//dd($filename);
// 2. Store the image on disk.
\Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream());
// 3. Save the path to the database
$this->attributes[$attribute_name] = $destination_path.'/'.$filename;
}
}
public function setImgheaderAttribute($value)
{
dd($value);
$attribute_name = "imgheader";
$disk = "uploads";
$destination_path = "carte_intestate";
// if the image was erased
if ($value==null) {
// delete the image from disk
\Storage::disk($disk)->delete($this->{$attribute_name});
// set null in the database column
$this->attributes[$attribute_name] = null;
}
// if a base64 was sent, store it in the db
if (starts_with($value, 'data:image'))
{
// 0. Make the image
$image = \Image::make($value)->encode('jpg', 90);
// 1. Generate a filename.
$filename = md5($value.time()).'.jpg';
//dd($filename);
// 2. Store the image on disk.
\Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream());
// 3. Save the path to the database
$this->attributes[$attribute_name] = $destination_path.'/'.$filename;
}
}
On another model it works fine so, since i basically copied and pasted the code it should upload the image and save the name on the database, but the mutator doesn't fire at all.