如何使用Vue获取与帖子相关的评论 [英] How to fetch comment associated with post using Vue
问题描述
我正在与Laravel建立一个论坛,正如标题所述,我想使用Vue.js从数据库中获取某条帖子的评论,并在我的显示页面上显示它们.
I am building a forum with Laravel, and as the title says I wanna fetch a comment left on a certain post from my database using Vue.js,and show them on my show page.
我正在使用下面的网址显示某个帖子
I am using the url below to show a certain post
Route::get('forums/{category_id}/{title}','ForumsController@show');
下面的代码不起作用,任何帮助将不胜感激.
The code below does not work.Any help would be really appreciated.
ps.我正在使用Vue.resource.
ps.I am using Vue.resource.
论坛管理员
Route::get('forums','ForumsController@index');
Route::get('forums/create','ForumsController@create');
Route::post('forums', 'ForumsController@store');
Route::get('forums/{category_id}/{title}','ForumsController@show');
Route::get('forums/{category_id}','ForumsController@showByCategory');
Route::get('forums/{id}/{title}/edit','ForumsController@edit');
Route::patch('forums/{id}/{title}','ForumsController@update');
Route::delete('forums/destroy','ForumsController@destroy');
Route::post('forums/{category_id}/{title}', 'ForumsController@saveReply');
//API
Route::get('api/posts/{category_id}/{title}', function($category_id, $title){
return App\Topic::with('comments')->where(compact('category_id','title'))->firstOrFail();
});
Vue
new Vue({
el: '#comment',
methods: {
fetchComment: function (category_id, title) {
this.$http.get('/api/posts/' + category_id + '/' + title ,function (data) {
this.$set('topics',data)
})
}
},
ready: function () {
this.fetchComment()
}
})
show.blade.php
show.blade.php
@extends('app')
@section('content')
<div id="comment">
<ul v-for="comment in posts">
<li>@{{comment.reply}}</li>
</ul>
</div>
@stop
张贴表格
public function up(){
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');$table->integer('category_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->string('title');
$table->text('body');
$table->timestamps();
});
评论表
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->text('reply');
$table->integer('user_id')->unsigned();
$table->integer('topic_id')->unsigned();
$table->foreign('topic_id')->refrenced('id')->on('topics')->onDelete('cascade');
$table->timestamps();
显示特定帖子的方法
public function show($category_id, $title)
{
$topic = Topic::where(compact('category_id','title'))->firstOrFail();
$comments = Comment::where('topic_id',$topic->id)->get();
return view('forums.category', compact('topic','comments'));
}
感谢杰夫帮助我
Thanks Jeff for helping me out
我这样修改了
fetchComment: function (category_id, title) {
this.$http.get('/api/topics/' + category_id + '/' + title ,function (data) {
this.$set('topics',data)
})
}
ready: function () {
this.fetchComment(category_id, title)
}
但是我收到此错误消息
http://my-sitename/api/topics/undefined/undefined 404 (Not Found)
我是一个完全菜鸟,所以对您的帮助将不胜感激
I am a complete noob, so any help will be really appreciated
推荐答案
您的函数fetchComment
带有2个参数,您不会发送任何参数.
Your function fetchComment
takes 2 arguments, you aren't sending it any.
fetchComment: function (category_id, title)
ready: function () {
this.fetchComment() //needs arguments
}
这篇关于如何使用Vue获取与帖子相关的评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!