使用ajax从多个表中检索数据

时间:2016-11-10 00:43:12

标签: ajax database laravel relational

这是帖子表:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');

            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

            $table->integer('prf_id')->unsigned();
            $table->foreign('prf_id')->references('id')->on('profiles')->onDelete('cascade');

            $table->longText('status');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('posts');
    }
}

这是评论表:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCommentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->increments('id');

            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

            $table->integer('prf_id')->unsigned();
            $table->foreign('prf_id')->references('id')->on('profiles')->onDelete('cascade');

            $table->integer('post_id')->unsigned();
            $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');

            $table->longText('comment');
            $table->integer('like')->unsigned();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('comments');
    }
}

此帖子模型

    <?php

    namespace App;

    use Illuminate\Database\Eloquent\Model;

    class Post extends Model
    {
        protected $table='posts';
        protected $fillable = ['status'];
    protected $hidden = [];

    public function profile(){
        return $this->belongsTo('App\Profile', 'prf_id');
    }

    public function user(){
        return $this->belongsTo('App\User');
    }

    public function comment(){
        return $this->hasMany('App\Comment');
    }


}

这是评论模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    protected $table='comments';
    protected $fillable = ['comment','like'];


    protected $hidden = [];

    public function post(){
        return $this->belongsTo('App\Post', 'post_id');
    }

    public function profile(){
        return $this->belongsTo('App\Profile', 'prf_id');
    }

    public function user(){
        return $this->belongsTo('App\User');
    }

}

在刀片页面中,我可以轻松检索特定帖子的所有评论,例如: 假设我有帖子的帖子

@foreach ($post->comment as $comment)
{{$comment->comment}}

但是在ajax中我怎么能这样做 假设我返回响应() - &gt; json($ posts); 有什么建议吗?它会对我有很大帮助

1 个答案:

答案 0 :(得分:0)

您不必编写response()->json($posts),您只需return $posts,Laravel就会自动将响应转换为JSON。

关于您的确切问题:在控制器中查询$posts时,添加with('comment')执行,例如:$posts = Post::with('comment')->get()它将返回带有注释预取的帖子。 这是Laravel的急切加载,你可以在这里阅读更多相关内容:https://laravel.com/docs/5.3/eloquent-relationships#eager-loading