lundi 14 mars 2016

How to fetch comment associated with post using Vue

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();



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire