Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

armancs's avatar

How can i display an image from database.

I want to add a background image in my view file with using css background property. the image will uploaded by user from admin and store it database. now how can i retrive the image in view file.

is this right way?

style="background-image: url({{URL::to('/')}}/images/university/{{ $unilist1->university_cover_img }});"

its display nothing.

i tried this one also:

background-image: url(/images/university/{{ $unilist1->university_cover_img }});
0 likes
9 replies
Shahrukh4's avatar

Actually this url() will retrieve the image path from route images/university/image which must be define in your web.php and there must be any controller logic which returns the full image path, only then this will work.Hope this will work.

vinodjoshi's avatar

Try it May be it can help

Route::get('storage/{imagename}', function ($imagename) { $path = storage_path('public/' . $imagename);

if (!File::exists($path)) {
    abort(404);
}

$file = File::get($path);
$type = File::mimeType($path);

$response = Response::make($file, 200);
$response->header("Content-Type", $type);

return $response;

})

armancs's avatar

Do you understand my question? i don't know what you gave? please can you explain@vinodjoshi

newbie360's avatar

can you show the url html source code, are you sure saved the file not the file name ?

if you want to show the image from db, i think still need to set the header content-type

i'm not sure, because never store image to db, what is table column type of the image

armancs's avatar

yes , obviously in database the filename stored and in folder the file is stored. @newbie360

armancs's avatar
armancs
OP
Best Answer
Level 2

Thanks everyone.I made it.

style="background-image: url({{URL::to('/images/university')}}/{{$unilist1->university_cover_img}});"
newbie360's avatar

stored the file in a folder ? or stored in database ? the column of image is LONGBLOB datatype ?

armancs's avatar

stored in database and move that file in a folder.no its not longblog its varchar. @newbie360

can you help me how can i make condition inside this. if image exist than it will display image other wise it will display a message image not found or content not found.

Please or to participate in this conversation.