sleeping-owl / with-join
将 Eloquent BelongsTo 子查询转换为带有左连接的一个查询的包。
1.0.11
2015-02-21 13:48 UTC
Requires
- php: >=5.4.0
- illuminate/support: >=4.1
- laravel/framework: >=5.0.6
Requires (Dev)
- illuminate/database: >=4.1
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
而不是默认的 Eloquent 行为执行 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 模型中使用
\SleepingOwl\WithJoin\WithJoinTrait
特性class StreetImage extends \Eloquent { use \SleepingOwl\WithJoin\WithJoinTrait; }
-
这就完了。
支持库
您可以通过 PayPal 或在 BTC:13k36pym383rEmsBSLyWfT3TxCQMN2Lekd 捐款
版权和许可
此包由 Sleeping Owl 为 Laravel 框架编写,并按 MIT 许可发布。有关详细信息,请参阅 LICENSE 文件。