批量显示关系数据
Show relationship data in bulk
我正在写一个 API 并且有我的主题和与之相关的评论。我的愿望是显示所有的主题,同时也显示属于该主题的评论。我用 hasMany 选项建立了关系,但我无法显示数据。
Post Table
评论Table
Post型号
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Posts extends Model
{
protected $table = 'posts';
protected $fillable = [
'post_id',
'user_id'
];
public function user(){
return $this->belongsTo('App\Models\Users');
}
public function comment()
{
return $this->hasMany('App\Models\Comments', 'post_id', 'id');
}
}
评论模型
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Comments extends Model
{
protected $fillable = [
'user_id',
'post_id',
'description'
];
public function user()
{
return $this->belongsTo('App\Models\Users', 'user_id', 'id');
}
public function post()
{
return $this->belongsTo('App\Models\Posts', 'post_id', 'id');
}
}
比如我在拍下面的所有话题的时候,我要的都是带用户id的评论。我该如何自定义它?
{
"id": 1,
"user_id": 10,
"image": "https://lorempixel.com/800/400/cats/WhaleDevops/?43757",
"description": "Et quia enim distinctio non qui laudantium voluptatem. Cumque minus cum pariatur necessitatibus. Repellat qui provident voluptatum ut. Et sapiente eaque eum ut. Repudiandae eveniet a harum ea totam consectetur. Facere facilis sunt et consequuntur sapiente. A aspernatur placeat tenetur et. Omnis quasi sunt nostrum et velit sint quia. Velit temporibus ut aut ea in repudiandae.",
"created_at": "1999-04-11T17:37:42.000000Z",
"updated_at": "2020-08-08T10:41:28.000000Z"
},
{
"id": 2,
"user_id": 2,
"image": "https://lorempixel.com/800/400/cats/WhaleDevops/?53905",
"description": "Quasi ab recusandae molestiae pariatur et ut. Harum dolorum illo aspernatur fugit sequi aut. Modi quasi voluptas ad maxime ducimus quia molestiae maiores. Pariatur quam quam officia expedita. Alias quas aliquid et.",
"created_at": "1972-03-17T04:01:26.000000Z",
"updated_at": "2020-08-08T10:41:28.000000Z"
},
您必须阅读 Laravel Resources
When building an API, you may need a transformation layer that sits between your Eloquent models and the JSON responses that are actually returned to your application's users. Laravel's resource classes allow you to expressively and easily transform your models and model collections into JSON.
文档写得很好,易于理解。它应该有你需要的一切。
您需要阅读 Writing Resources 章节,该章节解释了如何处理关系。
您必须进行两项更改。
1)从posttable中删除post_id并放在评论table.
中
2)编写查询
Post::with('comment','user')->get();
我正在写一个 API 并且有我的主题和与之相关的评论。我的愿望是显示所有的主题,同时也显示属于该主题的评论。我用 hasMany 选项建立了关系,但我无法显示数据。
Post Table
评论Table
Post型号
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Posts extends Model
{
protected $table = 'posts';
protected $fillable = [
'post_id',
'user_id'
];
public function user(){
return $this->belongsTo('App\Models\Users');
}
public function comment()
{
return $this->hasMany('App\Models\Comments', 'post_id', 'id');
}
}
评论模型
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Comments extends Model
{
protected $fillable = [
'user_id',
'post_id',
'description'
];
public function user()
{
return $this->belongsTo('App\Models\Users', 'user_id', 'id');
}
public function post()
{
return $this->belongsTo('App\Models\Posts', 'post_id', 'id');
}
}
比如我在拍下面的所有话题的时候,我要的都是带用户id的评论。我该如何自定义它?
{
"id": 1,
"user_id": 10,
"image": "https://lorempixel.com/800/400/cats/WhaleDevops/?43757",
"description": "Et quia enim distinctio non qui laudantium voluptatem. Cumque minus cum pariatur necessitatibus. Repellat qui provident voluptatum ut. Et sapiente eaque eum ut. Repudiandae eveniet a harum ea totam consectetur. Facere facilis sunt et consequuntur sapiente. A aspernatur placeat tenetur et. Omnis quasi sunt nostrum et velit sint quia. Velit temporibus ut aut ea in repudiandae.",
"created_at": "1999-04-11T17:37:42.000000Z",
"updated_at": "2020-08-08T10:41:28.000000Z"
},
{
"id": 2,
"user_id": 2,
"image": "https://lorempixel.com/800/400/cats/WhaleDevops/?53905",
"description": "Quasi ab recusandae molestiae pariatur et ut. Harum dolorum illo aspernatur fugit sequi aut. Modi quasi voluptas ad maxime ducimus quia molestiae maiores. Pariatur quam quam officia expedita. Alias quas aliquid et.",
"created_at": "1972-03-17T04:01:26.000000Z",
"updated_at": "2020-08-08T10:41:28.000000Z"
},
您必须阅读 Laravel Resources
When building an API, you may need a transformation layer that sits between your Eloquent models and the JSON responses that are actually returned to your application's users. Laravel's resource classes allow you to expressively and easily transform your models and model collections into JSON.
文档写得很好,易于理解。它应该有你需要的一切。
您需要阅读 Writing Resources 章节,该章节解释了如何处理关系。
您必须进行两项更改。
1)从posttable中删除post_id并放在评论table.
中2)编写查询
Post::with('comment','user')->get();