【问题标题】:Vue.js Get public folder images in laravel 5.8Vue.js 在 laravel 5.8 中获取公用文件夹图片
【发布时间】:2019-10-18 07:04:02
【问题描述】:

我正在创建评论系统,现在我想在评论区显示用户个人资料图片。

我想从 laravel 5.8 公共文件夹中捕获并显示多个图像。这是我尝试过的。

如果用户有个人资料图片,我想显示它。如果没有,我想展示 一个使用 vue 头像组件的头像。我的图像存储在 公开/上传/个人资料。

现在我的控制台中没有任何错误。我还可以显示用户名和用户 cmets。

图片名称存储在mysql中。

comment.vue(第一条评论)

<div class="user" v-if="comment.user.image && comment.user.image.length > 0">
                    <span :v-for="(item, index) in comment.user.image">
                        <img :src="'uploads/' + 'profile' + '/' + item.image">
                    </span>
                </div>
                <div else class="user" >
                    <avatar :username="comment.user.name" :size="45"></avatar>
                </div>

commentController.php

public function index(Post $post)
{
    return $post->comments()->paginate(10);
}

public function show(Comment $comment)
{
    return $comment->replies()->paginate(10);
}

public function store(Request $request, Post $post)
{
return auth()->user()->comments()->create([
    'body' => $request->body,
    'post_id' => $post->id,
    'comment_id' => $request->comment_id
])->fresh();
}

个人资料表

Schema::create('profiles', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('user_id');
        $table->string('image');
        $table->string('gender');
        $table->string('country');
        $table->string('bod');
        $table->string('instagram');
        $table->string('description');
        $table->timestamps();
    });

在我的 VS 代码中,上传文件夹开始变红。

【问题讨论】:

  • 检查你的imgsrc标签,看看页面渲染后生成了什么url
  • 您是否尝试过使用前面的'/'/uploads/...?在我的项目中,我总是需要前面的斜线。
  • '' + item.image' 看起来很连贯。
  • @skribe 我试过但没用
  • 正如@Vibha 所说,最终渲染的 src 属性是什么样的?

标签: php laravel vue.js get laravel-5.8


【解决方案1】:

我的解决方案是创建一个 api 路由来显示图像。例如。

api.php

Route::get('profile/{fileName}', 'ProfileController@showImage');

ProfileController.php

class ProfileController {
    ...
    public function showImage($fileName)
    {
        $path = public_path("/images/{$fileName}");

        if (!\File::exists($path)) {
            return response()->json(['message' => 'Image not found.'], 404);
        }

        $file = \File::get($path);
        $type = \File::mimeType($path);

        $response = \Response::make($file, 200);
        $response->header("Content-Type", $type);

        return $response;
    }

}

而您的图像 src 将是 /api/profile/{imageName}。这是我的解决方案。

【讨论】:

  • 谢谢,没用,所以我改了 public_path("/images/{$fileName}");通过 public_path("/uploads/profile/{$fileName}"); .我现在的新错误是:8000/api/profile/[object%20Object]:1 GET localhost:8000/api/profile/[object%20Object]404 (Not Found)
  • 这个路径只是举例。您应该自己编辑正确的路径。
  • 是的,我认为我的公共路径是 '/' + 'uploads' + '/' + 'profile' + '/' + $fileName 所以我做到了。但它仍然没有。无论如何,谢谢你的帮助。
  • 您是否在api.php 上创建了路线?
  • 还没有我只有 Route::middleware('auth:api')->get('/user', function (Request $request) { return $request->user(); } );
猜你喜欢
  • 2020-02-13
  • 1970-01-01
  • 2019-09-16
  • 1970-01-01
  • 1970-01-01
  • 2020-01-21
  • 1970-01-01
  • 2017-06-27
  • 1970-01-01
相关资源
最近更新 更多