【发布时间】:2020-02-13 01:33:16
【问题描述】:
我正在创建评论系统。 我在我的 laravel 项目中集成了 vue.js。
在我的评论区,我想在我的 laravel 中显示用户个人资料图片 公共文件夹。
但我不知道如何显示图像。
我想要实现的是,
如果用户有个人资料图片,我想在评论区显示它。但如果 用户没有头像,我想显示头像。
我使用了vue头像组件,所以我想我想用它。
我的个人资料图片存储在 public/uploads/profile。
comment.vue
<template>
<div>
<div class="reply-comment" >
<div class="user-comment">
<div class="user">
<!---<img src="{{ $comment->user->img }}" class="image-preview__image">--->
<avatar :username="comment.user.name" :size="45"></avatar>
</div>
<div class="user-name">
<span class="comment-name"><a :href=" '/user/' + 'profile' +'/'+ comment.user.id + '/' ">{{ comment.user.name }}</a></span>
<p>{{ comment.body }}</p>
</div>
</div>
</div>
<div class="reply">
<button @click="addingReply = !addingReply" class="reply-button" :class="{ 'red' : !addingReply, 'black' : addingReply }">
{{ addingReply ? 'Cancel' : 'Add Reply'}}
</button>
</div>
<div class="user-reply-area" v-if="addingReply">
<div class="reply-comment">
<input v-model='body' type="text">
</div>
<button @click="addReply" class="comment-button"><span>Add Reply</span></button>
</div>
<replies ref='replies' :comment="comment"></replies>
</div>
</template>
<script>
import Avatar from 'vue-avatar'
import Replies from './replies.vue'
export default {
components: {
Avatar,
Replies
},
data() {
return {
body: '',
addingReply: false
}
},
props: {
comment: {
required: true,
default: () => ({})
},
post: {
required: true,
default: () => ({})
}
},
methods: {
addReply() {
if(! this.body) return
axios.post(`/comments/${this.post.id}`, {
comment_id: this.comment.id,
body: this.body
}).then(({data})=> {
this.body = ''
this.addingReply = false
this.$refs.replies.addReply(data)
})
.catch(function (error) {
console.log(error.response);
});
}
}
}
</script>
profile.php
public function user(){
return $this->belongsTo(User::class, 'user_id');
}
user.php
public function profile(){
return $this->hasOne(Profile::class);
}
public function comments()
{
return $this->hasMany(Comment::class);
}
comment.php
protected $with = ['user'];
protected $appends = ['repliesCount'];
web.php
Route::get('results/{post}/comments', 'CommentController@index');
【问题讨论】:
-
图片路径怎么称呼?图片名称是保存在
database还是路径? -
@GayanS.Muthukumarana 对不起,你是什么意思?谢谢你的帮助。
-
我已经编辑了我的评论
-
@GayanS.Muthukumarana 谢谢,我已将文件名保存在 mysql 服务器中。
标签: javascript php laravel image vue.js