0
votes

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.

6
What is the exact content in the database cell? image1.jpeg? How did you manage to upload them? - Oliver Adria
I have updated store function as well in the description - Usha Priya

6 Answers

0
votes

You can do like so:

@foreach(explode('|',$tests->filenames) as $fileName)
    <img src="/storage/image/{{$fileName}}"/>
@endforeach
0
votes

If the images names are stored properly in the database with the correct extension, here's what you are going to need in your blade view:

  <img src="/storage/image/{{$tests->NAME_OF_COLUMN_IN_DB}}" />

just replace "NAME_OF_COLUMN_IN_DB" with the column name where you store the image name in your database.

0
votes

try like this <img src="/storage/image/{{ $tests['filenames'] }}" height="30px" width="30px" /> hope it will help: In This case: In controller use explode() to $tests->filenames make an array and then loop it like this

@foreach(json_decode($tests->filenames, true) as $images)
     <img src="/storage/image/{{ $images }}" height="30px" width="30px" />
@endforeach
0
votes

Use like this

<img src="{{ url('') }}/storage/image/{{$tests-> filenames}}">
0
votes

In Your Model Write a function like this. Suppose you are in User Model.

    public function getImage()
{
    return isset($this->image) && $this->image ? asset('userPhoto/'.$this->image): asset("dist/img/user2-160x160.jpg");
}

Now retrive the image in the blade file like this-

<a href="{{asset('userPhoto/'.$user->image)}}"target="_blank">
<img src="{{ $user->getImage() }}"width="50"></a>
-1
votes

You can follow the procedure below

<img src="{{ public_path('storage\image') }}{{ $tests->filenames }}" alt=""/>

Use the following code to separate the names of the images

$img = exploade('|', $tests->filenames);
dump($img);