【问题标题】:How to fetch comment associated with post using Vue如何使用 Vue 获取与帖子相关的评论
【发布时间】:2016-06-30 10:10:52
【问题描述】:

我正在使用 Laravel 建立一个论坛,正如标题所说,我想使用 Vue.js 从我的数据库中获取对某个帖子的评论,并将它们显示在我的展示页面上。

我正在使用下面的网址显示某个帖子

Route::get('forums/{category_id}/{title}','ForumsController@show');

下面的代码不起作用。任何帮助将不胜感激。

ps.我正在使用 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

@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'));
}

感谢杰夫帮助我

我是这样修改的

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)

我是一个完全的菜鸟,所以任何帮助将不胜感激

【问题讨论】:

    标签: laravel laravel-5 vue.js


    【解决方案1】:

    你的函数fetchComment 有两个参数,你没有发送任何参数。

    fetchComment: function (category_id, title)
    
    ready: function () {
      this.fetchComment() //needs arguments
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-09
      • 1970-01-01
      • 1970-01-01
      • 2012-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多