Vue.js Получить образы общих папок в laravel 5.8 - PullRequest
0 голосов
/ 18 октября 2019

Я создаю систему комментирования, теперь я хочу показывать изображения профиля пользователя в области комментариев.

Я хочу поймать и показать несколько изображений из общей папки laravel 5.8. Вот что я попробовал.

Если у пользователя есть изображение профиля, я хочу его показать. Если нет, я хочу показать аватар с использованием компонента vue avatar. Мои изображения хранятся в общедоступном / загружаемом / профиле.

Теперь у меня нет ошибок в моей консоли. Также я могу показать имя пользователя и комментарии пользователей.

Имя изображения хранится в 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 папка для закачек стала красной.

1 Ответ

1 голос
/ 18 октября 2019

Мое решение - создать маршрут 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;
    }

}

И ваш образбыть /api/profile/{imageName}. Это мое решение.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...