Laravel Backpack Image Field Error: 1406 Data too long for column
Laravel Backpack Image Field Error: 1406 Data too long for column
无法弄清楚为什么 CRUD 试图将图像二进制文件而不是上传文件的名称存储到数据库中。
型号
public function setImageAttribute($value)
{
$attribute_name = "image_wheel";
$disk = "wheel";
$destination_path = "wheel_png";
// if the image was erased
if ($value==null) {
// delete the image from disk
\Storage::disk($disk_wheel)->delete($this->{$image_wheel});
\Storage::disk($disk_vehicle)->delete($this->{$image_vehicle});
// set null in the database column
$this->attributes[$image_wheel] = null;
$this->attributes[$image_vehicle] = 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);
// 1. Generate a filename.
$filename = md5($value.time()).'.jpg';
// 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;
}
}
错误信息
Next Illuminate\Database\QueryException: SQLSTATE[22001]:
String data, right truncated: 1406 Data too long for column 'image_wheel' at row 1
(SQL: insert into wheels
(name
, part_no
, alias
, series_id
, sizes
, image_wheel
, image_vehicle
, description
, order
, status
, featured
, -wheel_image_id
, -wheel_tags
, updated_at
, created_at
) values (, , , , , data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/
...........
确保将 image_wheel
更改为 text
。您可能达到了该字段当前数据类型的最大长度。
函数名有问题。函数名应该有字段名。就像在您的示例中一样,它是
$attribute_name = "image_wheel";
所以函数名应该是
public function setImage_wheelAttribute($value)
这将解决问题
Uahmed 的回答让我找到了正确答案
根据 Laravel 的文档
定义增变器时
To define a mutator, define a setFooAttribute method on your model where Foo is the "studly" cased name of the column you wish to access.
所以在我的例子中函数名应该是
public function setImageWheelAttribute($value)
无法弄清楚为什么 CRUD 试图将图像二进制文件而不是上传文件的名称存储到数据库中。
型号
public function setImageAttribute($value)
{
$attribute_name = "image_wheel";
$disk = "wheel";
$destination_path = "wheel_png";
// if the image was erased
if ($value==null) {
// delete the image from disk
\Storage::disk($disk_wheel)->delete($this->{$image_wheel});
\Storage::disk($disk_vehicle)->delete($this->{$image_vehicle});
// set null in the database column
$this->attributes[$image_wheel] = null;
$this->attributes[$image_vehicle] = 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);
// 1. Generate a filename.
$filename = md5($value.time()).'.jpg';
// 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;
}
}
错误信息
Next Illuminate\Database\QueryException: SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'image_wheel' at row 1 (SQL: insert into
wheels
(name
,part_no
,alias
,series_id
,sizes
,image_wheel
,image_vehicle
,description
,order
,status
,featured
,-wheel_image_id
,-wheel_tags
,updated_at
,created_at
) values (, , , , , data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/ ...........
确保将 image_wheel
更改为 text
。您可能达到了该字段当前数据类型的最大长度。
函数名有问题。函数名应该有字段名。就像在您的示例中一样,它是
$attribute_name = "image_wheel";
所以函数名应该是
public function setImage_wheelAttribute($value)
这将解决问题
Uahmed 的回答让我找到了正确答案
根据 Laravel 的文档
定义增变器时To define a mutator, define a setFooAttribute method on your model where Foo is the "studly" cased name of the column you wish to access.
所以在我的例子中函数名应该是
public function setImageWheelAttribute($value)