【问题标题】:retrieve data from multiple table using ajax使用ajax从多个表中检索数据
【发布时间】:2017-03-23 22:24:36
【问题描述】:

这是帖子表:

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

这是 cmets 表:

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

}

在刀片页面中,我可以轻松检索特定帖子的所有 cmets,例如: 假设我有 $posts 作为帖子

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

但是在 ajax 中我怎么能做到这一点 假设我 return response()->json($posts); 有什么建议吗?对我有很大帮助

【问题讨论】:

  • 包含您的 JavaScript 代码

标签: ajax database laravel relational


【解决方案1】:

你不必写response()-&gt;json($posts),你可以简单地return $posts,Laravel 会自动将响应转换为 JSON。

关于您的确切问题:在控制器中查询$posts 时,添加with('comment') 执行,例如:$posts = Post::with('comment')-&gt;get() 然后它将返回带有预取评论的帖子。 这是 Laravel 的热切加载,您可以在此处阅读更多信息:https://laravel.com/docs/5.3/eloquent-relationships#eager-loading

【讨论】:

  • 我想使用 ajax 这就是为什么我写 response()->json($posts) 这个
猜你喜欢
  • 2016-12-29
  • 1970-01-01
  • 2013-10-02
  • 2019-04-30
  • 1970-01-01
  • 1970-01-01
  • 2022-08-14
相关资源
最近更新 更多