【问题标题】:No Image showing in a home Laravel没有图像显示在家庭 Laravel
【发布时间】:2020-05-27 23:22:03
【问题描述】:

在 laravel 中,我必须使用 php artisan storage:link 链接存储,这样我就可以显示图像,当用户发布带有图像的文章时,图像将被显示。但现在,图像没有出现。我该如何解决。

这是来自views/post/index.blade.php的代码

<tbody>
            @foreach($posts as $post)
            <tr>
                <td>
                    <img src="{{ asset($post->image) }}" alt="">
                </td>
                <td>
                    {{ $post->title }}
                </td>
                <td>
                    <a href=" {{ route('categories.edit', $post->category->id) }}">
                        {{ $post->category->name }}
                    </a>
                </td>

当用户发布图像时,图像将存储在我之前所做的链接存储中。所以这是postscontroller.php 中的代码,如果我将“posts”更改为“post”,它会显示错误。因为我不适合我之前写的'posts'功能。

public function store(CreatePostRequest $request)
{
    // upload image omegalul
    $image = $request->image->store('posts');
    // create the post
    $post = Post::create([
        'title' => $request->title,
        'description' => $request->description,
        'content' => $request->content,
        'image' => $image,
        'published_at' => $request->published_at,
        'category_id' => $request->category,
        'user_id' => auth()->user()->id,
    ]);

    if ($request->tags) {
        $post->tags()->attach($request->tags);
    }
    // flash a message
    session()->flash('success', 'Post created successfully');
    // redirect the user
    return redirect(route('posts.index'));
}

如果你们发现我没有出现在这里的任何问题,请问我。非常感谢你们

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    如果您正在使用 Laravel Storage,建议使用存储功能来存储、获取或删除媒体文件。

    使用这些命令来做到这一点:

    存储:

    Storage::disk(config('filesystems.default'))->put($path, $file);
    

    获得:

    Storage::disk(config('filesystems.default'))->url($file);
    

    删除:

    Storage::disk('local')->delete($path);
    

    你的代码会是这样的:

    <tbody>
            @foreach($posts as $post)
            <tr>
                <td>
                    <img src="{{ Storage::disk(config('filesystems.default'))->url($post->image) }}" alt="">
                </td>
                <td>
                    {{ $post->title }}
                </td>
                <td>
                    <a href=" {{ route('categories.edit', $post->category->id) }}">
                        {{ $post->category->name }}
                    </a>
                </td>
    

    还有:

    public function store(CreatePostRequest $request)
    {
        // upload image omegalul
        $image = Storage::disk(config('filesystems.default'))->put('posts', $request->image);
    

    注意:不要忘记检查您的文件系统是否设置为“本地” - 如果您使用的是本地存储。

    【讨论】:

      猜你喜欢
      • 2016-06-14
      • 1970-01-01
      • 2019-10-28
      • 1970-01-01
      • 1970-01-01
      • 2016-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多