Php 如何根据从另一外键显示的外键显示表格/Laravel 6
型号:Php 如何根据从另一外键显示的外键显示表格/Laravel 6,php,laravel,eloquent,Php,Laravel,Eloquent,型号: follow\u-up.id\u-priority是priority.id 控制器: public function followup() { return $this->hasMany('App\FollowUp', 'id_log', 'id') ->select(['id', 'id_log', 'request', 'id_priority']); } 返回的json数据片段: $test = DmLog::withTrashed() ->
follow\u-up.id\u-priority
是priority.id
控制器:
public function followup()
{
return $this->hasMany('App\FollowUp', 'id_log', 'id')
->select(['id', 'id_log', 'request', 'id_priority']);
}
返回的json数据片段:
$test = DmLog::withTrashed()
->with('followup')
->with('department')
->with('membership')
->with('room')
->with('roommove')
->with('communication')
->with('category')
->with('userCreator')
->with('userUpdator')
->find($id);
您能告诉我如何根据外键显示表格优先级吗?外键是id\u priority
?
好的。。。这是一个误会,所以我想得到的结果是:
[{...},
"created_at": "2020-12-29T18:14:26.000000Z",
"updated_at": null,
"deleted_at": null,
"followup": [
{
"id": 2,
"id_log": 1,
"request": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.",
"id_priority": 2
},
{
"id": 3,
"id_log": 1,
"request": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.",
"id_priority": 1
}
],
"department": {
"id": 2,
"department": "Kitchen"
},
{...}]
您可以使用方法调用
上的闭包函数:
[{...},
"created_at": "2020-12-29T18:14:26.000000Z",
"updated_at": null,
"deleted_at": null,
"followup": [
{
"id": 2,
"id_log": 1,
"request": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.",
"id_priority": 2
"priority": {
"id": 2
"priority": "not urgent"
}
},
{
"id": 3,
"id_log": 1,
"request": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.",
"id_priority": 1,
"priority": {
"id": 1
"priority": "urgent"
}
}
],
"department": {
"id": 2,
"department": "Kitchen"
},
{...}]
如果你在followup模型中定义了优先级关系,你可以通过调用->with('followup.priority')轻松加载它,也可以通过传递数组->with['followup','followup.priority',…]将所有关系组合在一起,这是一个误解,我用我想要得到的结果编辑了我的帖子,因此更容易理解,感谢您的回答:)如果尚未定义,您可以在Followup模型上定义priority
关系,然后使用('Followup.priority')立即加载
我没有这样做,但您的评论很有帮助,谢谢!有一个误会,我编辑了我的帖子,结果是我想要得到的,所以更容易理解,谢谢你的回答:)@Bernard我更新了我的答案。我没有这样做,但你的答案很有帮助,谢谢@伯纳德,不客气
use Illuminate\Database\Eloquent\Relations\BelongsTo;
$test = DmLog::withTrashed()
->with([
'followup' => function (BelongsTo $relationship) {
$relationship->select(['id', 'priority']);
},
]);