n0n0n0n0 / with-join
将 Laravel 5+ 中的 Eloquent BelongsTo 子查询转换为带左连接的单一查询的软件包。
2.0.0
2017-11-10 10:52 UTC
Requires
- laravel/framework: >5.0.0
README
用法
使用 ->references($relations) 方法或 Model::includes($relations) 方法将你想转换为左连接的关系标记
Model::with('other')->references('other')->orderBy('other.title', 'asc')->get(); # this will make one sql-query with left join of 'other' relation # result object will be the same object Model::includes('first', 'second')->where('first.title', '=', 'my title')->get(); # will be the same as Model::with('first', 'second')->references('first', 'second')->… Model extends Eloquent { $includes = ['first', 'second']; } Model::where('first.title', '=', 'my title')->get(); # result is same as Model::includes but definition is done within the model # if you use $with and $includes together it will be merged Model::with('foreign')->orderBy('field', 'asc')->get(); # this will work with default behaviour (perform 2 sql-queries)
示例
新行为
StreetImage::includes('street')->first()
将执行以下 SQL 查询
select `street`.`<…>` as `__f__street---<…>`, `street_images`.* from `street_images` left join `streets` as `street` on `street`.`id` = `street_images`.`street_id` order by `sort` asc limit 1
默认行为
StreetImage::with('street')->first()
将执行以下 SQL 查询
select `street_images`.* from `street_images` order by `sort` asc limit 1 select `streets`.* from `streets` where `streets`.`id` in (?) order by `title` asc
对象结构
如果你使用 includes() 方法,你将得到相同的对象。例如
StreetImage::includes('street')->first()
将返回
object(StreetImage) {
<all street image attributes>
street: object(Street) {
<all street attributes>
}
}
即使使用嵌套关系,结构也将保持不变
StreetImage::includes('street.district')->first();
将返回
object(StreetImage) {
<all street image attributes>
street: object(Street) {
<all street attributes>
district: object(District) {
<all district attributes>
}
}
}
嵌套关系
StreetImage::includes('street.type', 'street.district')->first();
将执行以下 SQL 查询 (<…> 将被替换为所有表列)
select `street`.`<…>` as `__f__street---<…>`, `type`.`<…>` as `__f__street---__f__type---<…>`, `district`.`<…>` as `__f__street---__f__district---<…>`, `street_images`.* from `street_images` left join `streets` as `street` on `street`.`id` = `street_images`.`street_id` left join `street_types` as `type` on `type`.`id` = `street`.`street_type_id` left join `districts` as `district` on `district`.`id` = `street`.`district_id` order by `sort` asc limit 1
而不是默认情况下执行 4 个 SQL 查询
select `street_images`.* from `street_images` order by `sort` asc limit 1 select `streets`.* from `streets` where `streets`.`id` in (?) order by `title` asc select * from `street_types` where `street_types`.`id` in (?) order by `title` asc select * from `districts` where `districts`.`id` in (?) order by `sort` asc
安装
-
在你的 composer.json 中添加此软件包并运行 composer update(或直接运行
composer require sleeping-owl/with-join:1.x)"sleeping-owl/with-join": "1.*" -
在每个你想要使用此软件包功能的 Eloquent 模型中使用
\Nonono\WithJoin\WithJoinTrait特性class StreetImage extends \Eloquent { use \Nonono\WithJoin\WithJoinTrait; }
-
这就结束了。
版权和许可
该软件包由 Sleeping Owl 为 Laravel 框架编写,并发布在 MIT 许可下。有关详细信息,请参阅 LICENSE 文件。