Trouble printing out image from public folder laravel

I want to show the image that I had just uploaded and only show it to those who have uploaded it. For example, user table contain jack, Emily and John, so if jack were to upload a file the image will show directly under him, but I don’t know how to do it?

This is how it look like now:
enter image description here

Controller: (how I store the image)

public function store1(Request $request){

   $this->validate($request, [
        'file' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);

   if ($request->hasFile('file')) {
        $image = $request->file('file');
        $name = $image->getClientOriginalName();
        $size = $image->getClientSize();
        $destinationPath = public_path('/images');
        $image->move($destinationPath, $name);

        $userImage = new UserImage;
        $userImage->name = $name;
        $userImage->size = $size;
        //dd($userImage);
        $userImage->save();
}

view.blade.php
 @foreach ($data as $object)
    <b>Name: </b>{{ $object->name }}<br><br>
@endforeach

I saw people using this inside their blade.php, but I don’t know what the $model is:
    <img src="{{ asset('public/images/' . $model->image) }}">

Upload.blade.php (this is my upload page where user will upload their image)

                        {{  csrf_field()  }}


<div class="form-group">
    <label for="imageInput" class="control-label col-sm-3">Upload Image</label>
            <div class="col-sm-9">
                <input type="file" name="file">

        </div>
    </div>

 <div class="form-group">
            <div class="col-md-6-offset-2">
              <input type="submit" class="btn btn-primary" value="Save">
            </div>
          </div>
          </form>

enter image description here

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

There are many ways to do it.

You could either use Eloquent or using query builder of Laravel.

In your controller you should get all the images that the user uploaded.

Query builder approach :

//don't forget the namespace
`use DB;`

//in your function write this.
$images = DB::table('user_images')
        ->join('users', 'users.id', '=', 'user_images.user_id')
        ->where('users.id', '=', $id) 
        ->get();

//use dd($images) to verify that the variable $images has data

//send $images in your view

in your view write a foreach loop like so:
@foreach($images as $image)
    <img src="{{ asset('public/images/' . $image->name ) }}"> 
@endforeach


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x