【问题标题】:Display image in a view Laravel 6在视图中显示图像 Laravel 6
【发布时间】:2020-12-04 10:52:49
【问题描述】:

我无法在视图中的标签 img 中显示我的图像。我可以将上传的图片保存在 storage->app->public->books 中,并将图片名称也保存在我的数据库中。但是在我运行 php artisan storage:link 以访问我在 public->storage 中的文件之后,但无论我使用asset() 或 url() 方法,我都可以' t 在 img 标签中显示我的图像,只是 alt="" 属性,有人知道我在做什么错吗?

这是我的控制器 store()。

public function store(Request $request)
{
    // Handle File Upload
    if($request->hasFile('book_image')){
        // Get filename with the extension
        $filenameWithExt = $request->file('book_image')->getClientOriginalName();
        // Get just filename
        $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
        // Get just ext
        $extension = $request->file('book_image')->getClientOriginalExtension();
        // Filename to store
        $fileNameToStore= $filename.'_'.time().'.'.$extension;
        // Upload Image
        $path = $request->file('book_image')->storeAs('books', $fileNameToStore);
    } else {
        $fileNameToStore = 'noimage.png';
    }
    //save in database
    $itens = \App\Book::create([
        'book_name' => mb_strtolower($request->book_name),
        'author_name' => mb_strtolower($request->author_name),
        'publishing_name' => mb_strtolower($request->publishing_name),
        'description' => $request->description,
        'release_date' => $request->release_date,
        'category' => mb_strtolower($request->category),
        'book_image' => $fileNameToStore
    ]);

    flash('Livro criado com sucesso !')->success();

    return redirect()->route('admin.books.index');
}

这是我要显示的 html。

   @foreach($books as $book)
    <tr>
        <td><input type="checkbox" class="checkthis" /></td>
        <td>
            <img style="width: 80px;height: 80px" src="{{ url('books/{$book->book_image}') }}" alt="{{$book->book_image}}">
        </td>
        <td>{{$book->book_name}}</td>
        <td>{{$book->author_name}}</td>
        <td>{{$book->publishing_name}}</td>
        <td>{{$book->release_date}}</td>
        <td>
   </tr>

还有我的配置->filesystems.php

    'disks' => [

    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
    ],

    'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ],

    's3' => [
        'driver' => 's3',
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION'),
        'bucket' => env('AWS_BUCKET'),
        'url' => env('AWS_URL'),
        'endpoint' => env('AWS_ENDPOINT'),
    ],

],

【问题讨论】:

    标签: php html laravel image view


    【解决方案1】:

    你需要传递 src 如下。

     src="{{ url('books/'.$book->book_image) }}"
    

    作为存储文件夹

    src="{{ asset('storage/books/'.$book->book_image) }}"
    

    【讨论】:

    • 非常感谢它为我工作!我使用了 src="{{ asset('storage/books/'.$book->book_image) }}" 并得到了它。我想我必须回到 php 连接变量 rsrs。
    猜你喜欢
    • 1970-01-01
    • 2017-02-17
    • 2021-05-30
    • 2020-09-13
    • 2019-03-18
    • 2016-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多