在 Laravel 8 中显示存储在 public 文件夹中的图像

Display image that are stored in public folder in Laravel 8

您好,我已经将图像存储在 public 文件夹中,但它不显示任何内容!有人帮我吗? (PS:我用输入文件的形式添加图像) Blade 文件:

@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" class="text-center">No post found.</td>
            </tr>
        @endforelse

控制器文件存储功能:

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

试试这个

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

您目前正在将您输入的文件上传的临时位置保存到新 post。您需要稍微重新安排您的函数以获取输入,然后在创建 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.');

}