Reputation: 802
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.I am using Vue.resource.
forum controller
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
@extends('app')
@section('content')
<div id="comment">
<ul v-for="comment in posts">
<li>@{{comment.reply}}</li>
</ul>
</div>
@stop
post table
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();
});
comment table
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();
method to show a certain post
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
I revised it like this
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)
}
But I get this error message
http://my-sitename/api/topics/undefined/undefined 404 (Not Found)
I am a complete noob, so any help will be really appreciated
Upvotes: 0
Views: 508
Reputation: 25221
Your function fetchComment
takes 2 arguments, you aren't sending it any.
fetchComment: function (category_id, title)
ready: function () {
this.fetchComment() //needs arguments
}
Upvotes: 0