带有关系的响应JSON-Laravel / Eloquent

时间:2018-09-05 23:03:57

标签: laravel rest api laravel-5 laravel-5.6

我正在研究Laravel项目,我想为网站创建REST API。在我的系统上,我有两个表:

博客和类别。表格博客具有category_id列,这是引用类别表中列ID的键。

博客迁移

class CreateBlogsTable extends Migration
{
    public function up()
    {
     Schema::create('blogs', function (Blueprint $table) {
     $table->increments('id');
     $table->string('title');
     $table->longtext('body');
     $table->string('category_id');
     $table->timestamps();
    });
   }
   .....
}

类别迁移

class CreateCategoriesTable extends Migration
{
    public function up()
    {
     Schema::create('categories', function (Blueprint $table) {
     $table->increments('id');
     $table->string('name');
     $table->timestamps();
    });
   }
   ....
}

我的博客模型

class Blog extends Model
{
    protected $fillable = ['title', 'body', 'category_id'];
    public function category() {
       return $this->hasMany('app\Category');
    }
}

我的博客模型

class Category extends Model
{
    protected $fillable = ['name'];
    public function blog() {
       return $this->hasMany('app\Blog');
    }
}

因此,我创建了BlogController并配置了访问相应API函数的路由。

api / blogs /通过GET用于我的控制器的索引功能,该功能如下所示:

public function index()
{
    $blog = Blog::all();
    return response()->json($blog, 200);
}

有了这个,我可以从blogs表中获取数据

[
    {
        "id": 1,
        "title": "title from my blog",
        "text": "text body here",
        "category_id": "2",
        "created_at": "2018-09-05 21:08:21",
        "updated_at": "2018-09-05 21:08:21"
    }
]

但是我想合并博客和类别表,并对此做出类似的回应

[
    {
        "id": 1,
        "title": "title from my blog",
        "text": "text body here",
        "category_id": "2",
        "created_at": "2018-09-05 21:08:21",
        "updated_at": "2018-09-05 21:08:21"
        "category": [{
           "id": 2,
           "name": "Development"
        }]
    }
]

有人帮忙吗?

3 个答案:

答案 0 :(得分:0)

加载关系,它将包含在序列化响应中。

$blog = Blog::with('category')->get();

此示例急于加载类别关系。

我建议您通读雄辩的关系文档:https://laravel.com/docs/5.6/eloquent-relationships

答案 1 :(得分:0)

我有解决方案,我只想说博客属于一个类别,一个类别可以有多个博客

class Blog extends Model
{
    protected $fillable = ['title', 'body', 'category_id',];
    public function category()
    {
        return $this->belongsTo('App\Category');
    }
}

类别模型

class Category extends Model
{
    protected $fillable = ['name'];
    public function blogs() {
        return $this->hasMany('App\Blog');
    }
}

因此要列出具有类别详细信息的博客

public function index()
{
    $blogs = Blog::with('category')->get();
    return response()->json($blogs, 200);
}

所以要在博客中列出类别

public function index()
{
    $categories = Category::with('blogs')->get();
    return response()->json($categories, 200);
}

答案 2 :(得分:0)

您正在使用hasMany关系,但类别表中的博客没有参考ID,因此不会急于加载数据。如果您将迁移更改为

 class CreateCategoriesTable extends Migration
  {
     public function up()
     {
       Schema::create('categories', function (Blueprint $table) {
         $table->increments('id');
         $table->string('name');
         $table->timestamps();
         $table->integer('category_id');
      });
    }
   ....
  }

然后您可以使用

Blog::with('category')->get();

或将关系更改为

public function category(){
 return $this->belongsTo(Category::class);
}

并使用

Blog::with('category')->get();