Kingdom Technology
Kingdom Technology

Reputation: 1

how to display uploaded image in laravel 7

i am trying to display my uploaded image in my view please anyone here to help me

controller

public function store(Request $request)
{
    $data = request()->validate([
        'another' => '',
        'caption'=> 'required',
        'image' => ['required', 'image',],
    ]);
    $imgpath=request('image')->store('uploads','public');
    $post = new Post([
        'caption' => $request->get('caption'),
        'image' => $request->get('imgpath'),
    ]);
    $user = Auth::user();
    $post = $user->posts()->create($request->all());
    $user->posts()->save($post);

    return redirect('/profile/'.auth()->user()->id);
}

this is what i use in my view

 <div class="row pt-4">
        @foreach($user->posts as $post)
            <div class="col-4">
                <img class="w-100" src="/storage/uploads/{{$post->image}}">
            </div>
        @endforeach

    </div>

Upvotes: 0

Views: 262

Answers (2)

Denis
Denis

Reputation: 45

If you are sure that this image is saved and it is saved in the desired directory try to use storage_path().

    @foreach($user->posts as $post)
        <div class="col-4">
            <img class="w-100" src="{{storage_path('uploads/' . $post->image)}}">
        </div>
    @endforeach

@Harout FMD is right. upload() method accepts two params. The second one specify the directory and since your param is 'public' the asset() method should do the work

Upvotes: 1

Harout
Harout

Reputation: 174

This should do it for you!

@foreach($user->posts as $post)
        <div class="col-4">
            <img class="w-100" src="{{asset('storage/'uploads/' . $post->image)}}">
        </div>
    @endforeach

Upvotes: 0

Related Questions