Home > front end >  Display image that are stored in public folder in Laravel 8
Display image that are stored in public folder in Laravel 8

Time:05-20

Hello I already stored my images in the public folder but it doesn't display anything ! Someone to help me please ? (PS: I add image with a form the input file) Blade File:

@forelse ($data as $key)
            <tr>
                <th scope="row">{{ $loop->index   1 }}</th>
                <td>{{ $key->title }}</td>
                <td><image style="width:70px;" src="{{ asset('public/Image/'.$key->image) }}"/></td>
                    <td></td>
            </tr>
        @empty
            <tr>
                <td colspan="4" >No post found.</td>
            </tr>
        @endforelse

Controller File store function:

public function store(Request $request)
{
    $post = new Post;
    $this->validate($request, [
        'title' => 'required',
        'image' => 'required',
        'description' => 'required',
        'price' => 'required',
        'category' => 'required',
        'location' => 'required',
        'state' => 'required',
        'image.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
    ]);

    if($request->hasFile('image'))
    {

        $file= $request->file('image');
        $extension= $file->getClientOriginalExtension();
        $filename = time().'.'.$extension;
        $file-> move('public/Image/', $filename);
        $post->image = $filename;

    }
     
    $input = $request->except(['_token']);
    $input = $request->all();
    $input['category'] = $request->input('category');
    $input = $request->all();
    $input['state'] = $request->input('state');
    Post::create($input);

    return redirect()->route('posts.index')
        ->with('success','Post created successfully.');


        
}

My images are stocked here

CodePudding user response:

Try this one

<image style="width:70px;" src="{{url('public/Image/'.$key->image')}}"/>

CodePudding user response:

You're currently saving the temporary location of the file upload coming from your input into the new post. You need to rearrange your function a bit to grab the input, then save the filename into the input before you create the post.

public function store(Request $request)
{
    $this->validate($request, [
        'title' => 'required',
        'image' => 'required',
        'description' => 'required',
        'price' => 'required',
        'category' => 'required',
        'location' => 'required',
        'state' => 'required',
        'image.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
    ]);
    
    $input = $request->except(['_token']);

    if($request->hasFile('image'))
    {

        $file= $request->file('image');
        $extension= $file->getClientOriginalExtension();
        $filename = time().'.'.$extension;
        $file-> move('public/Image/', $filename);
        $input['image'] = $filename; // Save the new and correct file name into the input

    }

    Post::create($input);

    return redirect()->route('posts.index')
        ->with('success','Post created successfully.');

}
  • Related