Lets see you controller method where you're generating your viewing and passing in $images...
Feb 11, 2016
9
Level 2
Retrieve image from database
Hello, I managed to save image to database and my problem now is how can I retrieve the image in my view? I am not using Image Intervention just a simple code only.. This is my Controller
$data = $request->all();
if(Input::hasFile('file')){
$file = Input::file('file');
$filename = $file->getClientOriginalName();
$image = Image::create([
'image_type' => $data['image_type'],
'link_id' => $data['link_id'],
'image_filename' => $filename,
'image_file' => $file,
'image_size'=>$file->getSize(),
'image_mime'=>$file->getMimeType(),
'auid' => Auth::user()->id,
]);
}
and to my migration / table
Schema::create('images', function (Blueprint $table) {
$table->increments('id');
$table->string('image_type', 4);
$table->string('image_filename', 100);
$table->binary('image_file');
$table->integer('image_size');
$table->string('image_mime');
$table->integer('link_id');
$table->integer('auid')->nullable()->default(null);
$table->integer('uuid')->nullable()->default(null);
$table->timestamps();
});
In my view when I execute the project what I get is "/tmp/phpHbFo3m"
@foreach($images as $image)
{{$image->image_file}}
@endforeach
And also I can already pass the data to my view, all I want to know now is how to retrieve the image in my view.. Any help will be appreciated :)
Level 2
Thank you for your response, I got wrong in upload image and viewing, and fixed it already so here is my codes.. Controller
if(Input::hasFile('file')){
$file = Input::file('file');
$filename = $file->getClientOriginalName();
$image = Image::create([
'image_type' => $data['image_type'],
'link_id' => $data['link_id'],
'image_filename' => $filename,
'image_file' => base64_encode(file_get_contents($file->getRealPath())), //added codes
'image_size'=>$file->getSize(),
'image_mime'=>$file->getMimeType(),
'auid' => Auth::user()->id,
]);
}
and in my view
@foreach($images as $image)
<img src="data:{{$image->image_mime}};base64,{{$image->image_file}}"/>
@endforeach
Please or to participate in this conversation.