简体   繁体   中英

Implicit conversion from data type varchar(max) to varbinary(max) is not allowed. Use the CONVERT function to run this query

I'm using MySQL in development, storing base64 image in longblob data type.

But I'm getting error below when trying to insert data after I changed database to MS SQL & data type to varbinary(max)

Implicit conversion from data type varchar(max) to varbinary(max) is not allowed. Use the CONVERT function to run this query

So I put a mutator to get this around as below,

public function setItemPictureAttribute($value)
{
    $this->attributes['item_picture'] = DB::raw('CONVERT(VARBINARY(MAX), "'. $value .'")');
}

but I'm getting error below when trying to store the base64 image;

The identifier that starts with '/9j/4QxnRXhpZgAATU0AKgAAAAgABwESAAMAAAABAAEAAAEaAAUAAAABAAAAYgEbAAUAAAABAAAAagEoAAMAAAABAAMAAAExAAIAAAAeAAAAcgEyAAIAAAAUAAAAkIdp' is too long. Maximum length is 128`

What did I miss?

EDIT: Somehow I got this work after I changed to $this->attributes['item_picture'] = DB::raw("CONVERT(VARBINARY(MAX), '". $value ."')"); ; notice that I switched the double quote with single quote and vice versa in DB::raw . Maybe someone can explain why

Somehow I got this work after I changed to

$this->attributes['item_picture'] = DB::raw("CONVERT(VARBINARY(MAX), '". $value ."')");; 

notice that I switched the double quote with single quote and vice versa in DB::raw . Maybe someone can explain why

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM