sql-serverlaravelvarbinarymax

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


Solution

  • 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