通过 orm 对涉及 an:m 关系的模型进行查询的最智能方法
我需要从表中提取一些数据;
最简洁的方式:
$levels = Cga_company_level::where(['company_uuid' => $companyUuid])->get()->pluck('cgaLevel');
第二简洁的方式:
$levels = Cga_company_level::where(['company_uuid' => $companyUuid])->get();
$levelIds= $companyLevelsByUuids->pluck('cga_level_id');
$levels= Cga_level::whereIn('cga_level_id',$levelIds)->get();
老派方式:
$companyLevelsByUuids = Cga_company_level::where(['company_uuid' => $companyUuid])->with('cgaLevel')->get();
$levels = [];
foreach ($companyLevelsByUuids as $companyLevel) {
$level = $companyLevel->cgaLevel;
$levels[] = $level;
}
任何其他最聪明的方法?
I need to extract some datas from a table;
the cleanest way:
$levels = Cga_company_level::where(['company_uuid' => $companyUuid])->get()->pluck('cgaLevel');
the second cleanest way:
$levels = Cga_company_level::where(['company_uuid' => $companyUuid])->get();
$levelIds= $companyLevelsByUuids->pluck('cga_level_id');
$levels= Cga_level::whereIn('cga_level_id',$levelIds)->get();
the old school way:
$companyLevelsByUuids = Cga_company_level::where(['company_uuid' => $companyUuid])->with('cgaLevel')->get();
$levels = [];
foreach ($companyLevelsByUuids as $companyLevel) {
$level = $companyLevel->cgaLevel;
$levels[] = $level;
}
any other smartest way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
levels
关系通过不同的方式获得它,但它不一定更有效。另外,您将通过 Id 查找公司,或者在以下查询中使用whereUuid($companyUuid)->first()
:You can use the
levels
relationship to get it through a different way but it might not necessarily be more efficient. Plus, you'll be finding the company through the Id or alternatively usewhereUuid($companyUuid)->first()
in the following query: