我有一个使用背包的laravel应用。 在我的用户表单上,我有两个图像字段,图像应上载到文件夹并在数据库中保存名称。 但是,我在用户模型上添加的增变器似乎根本没有触发。
新创建的用户保存在数据库中,但是两个长文本字段(用于图像名称)保存为null。
我在另一个模型上具有相同的机制,在这里工作正常。我尝试查看字段名称是否不一致,以及是否正确声明了磁盘,但一切似乎很好
这是我在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');
这些是我在Backpackuser模型中拥有的变种器:
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;
}
}
在另一个模型上,它可以正常工作,因为我基本上复制并粘贴了代码,所以它应该上传图像并将名称保存在数据库中,但增变器根本不会触发。