the format of image column is binary. you need to encode it as base64 to use it.
just you need is a getter. you can accomplish this using cast or simle accesser.
accesser
class Product extends Model
{
use HasFactory;
public function getImageAttribute($value)
{
return 'data:image/png;base64,' . base64_encode($value);
}
}
now you have the base64 format of image and can use it in src attribute of image element . or even you can store the base64 string in file system.
2
Answers
Because your
product->image
contains bytes content, encoder fromresponse
can’t properly deserialise it.You can’t pass pure byte code in a json response, use some kind of casting, read in docs "Eloquent: Mutators & Casting"
Ideally you should create a custom casting which will transform your blob data into a transportable form:
base64
,hex
, whatever you like best.Here’s an example for you
the format of
image
column is binary. you need to encode it as base64 to use it.just you need is a getter. you can accomplish this using cast or simle accesser.
accesser
now you have the base64 format of image and can use it in
src
attribute of image element . or even you can store the base64 string in file system.