Skip to content
Advertisement

display images from database to blade in laravel

I have a database where images are stored in the table row like this:

name(string)        "name"
sometext(string)    "some description"
filesnames(string)  image1.jpeg|image2.jpeg 

image1 and image2 are stored in the “public/storage/image” folder

public function store(Request $request)
{
    $data = request()->validate([
        'name' => 'required',
        'sometext' => '',
        'testImages' => '',
        'testImages' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);

    $images = array();
    
    if($files = $request->file('testImages')){
        foreach($files as $file){
            $name = $file->getClientOriginalName();
            $file->move('image', $name);
            $images[] = $name;
        }
    }

    auth()->user()->tests()->create([
        'name' => $data['name'],
        'sometext' => $data['sometext'],
        'filenames' => implode("|", $images),
    ]);
}

public function show ($id)
{
    $tests = Tests::findorfail($id);  //get row data from table with ID
    return view('test.show-report', compact('tests'));  //return data to blade
}

In blade I am able to do a $tests->name and $tests->description to get data successfully.

I am unable to figure out how to display images from “filenames” blade.

Advertisement

Answer

You can do like so:

@foreach(explode('|',$tests->filenames) as $fileName)
    <img src="/storage/image/{{$fileName}}"/>
@endforeach
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement