[英]Laravel Eloquent get relation field
I have a need to get a field from the relation from within the eloquent object.我需要从 eloquent 对象中的关系中获取一个字段。
I am using datatables to pass the eloquent object across for viewing.我正在使用数据表传递雄辩的对象以供查看。
I have an Model called Offices and a Model called Regions我有一个名为 Offices 的模型和一个名为 Regions 的模型
Offices id name region -> belongsto regions办公室 id 名称区域 -> 属于区域
Regions id name区域 ID 名称
I need to get something like this: Offices.id, Offices.name, Regions.name我需要得到这样的东西:Offices.id、Offices.name、Regions.name
I have tried the below but that fails, how would I get the regions name in the get method?我尝试了以下方法,但失败了,如何在 get 方法中获取区域名称?
$ReportData = Offices::with('region')->get(['id', 'name', 'region.name']);
return datatables()->eloquent($ReportData)
Eloquent relationships don't use joins, so you aren't able to select them in the Offices query. 口才的关系不使用联接,因此您无法在Offices查询中选择它们。
$ReportData = Offices::with('region')->get(['id', 'name']);
Will execute a separate query to eager load all of the regions while allowing you to access each item's related region as a property: 将执行一个单独的查询以渴望加载所有区域,同时允许您将每个项目的相关区域作为属性进行访问:
$ReportData[0]->region->name
You can flatten this, use a map, or use a query with a join like Tim suggested if you desire. 您可以根据需要展平,使用地图或对带有类似Tim的联接的查询使用。
You are able to limit the region data selected from the database by using a closure on with: 您可以通过对以下内容使用闭包来限制从数据库中选择的区域数据:
$ReportData = Offices::with(['region' => function($q) {
$q->select('name');
})->get(['id', 'name']);
However, in most cases, I would consider this as over-optimization unless the region has a lot of data in other columns. 但是,在大多数情况下,除非该区域在其他列中有大量数据,否则我认为这是过度优化。
To limit your query results to specific columns, and include that of a relationship, you can use ->join()
, coupled with ->select()
: 要将查询结果限制为特定的列并包括某种关系,可以使用->join()
以及->select()
:
$reportData = Offices::join("regions", "offices.region_id", "=", "regions.id")
->select("offices.id", "offices.name", "regions.name AS region_name")
->get();
Note: Had to guess on the join
logic; 注意:必须猜测join
逻辑; could be inverse ( regions.office_id
, etc) 可以取反( regions.office_id
等)
Then, when accessing the contents of $reportData
, you will only have access to three columns: 然后,当访问$reportData
的内容时,您将只能访问三列:
foreach($reportData AS $data){
$data->id;
$data->name;
$data->region_name;
}
Since this is rated first on google and this is what I got when I ran this search, I think it should also contain the Laravel 8 answer.由于这是在谷歌上排名第一的,这是我运行此搜索时得到的,我认为它还应该包含 Laravel 8 答案。 From the Laravel docs来自Laravel 文档
You can specify columns when eager loading relationships like so:您可以在急切加载关系时指定列,如下所示:
$ReportData = Offices::with('region:id, name')->get(['id', 'name']);
Mind you, you should always include the id and foreign id columns in this specifications, otherwise it won't work.请注意,您应该始终在本规范中包含 id 和 foreign id 列,否则它将不起作用。
So it should actually look something like this:所以它实际上应该是这样的:
$ReportData = Offices::with('region:id, name, office_id')->get(['id', 'name']);
Or whatever corresponds to your specific relationship.或者任何与你的特定关系相对应的东西。
Hopefully you find this useful, ye brave Laravel developer stumbling upon this!希望你发现这个有用,勇敢的 Laravel 开发人员偶然发现了这个!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.