sbamtr / laravel-query-enrich
Laravel eloquent 和查询构建器助手
1.2.0
2024-03-17 18:44 UTC
Requires
- illuminate/contracts: ^8.0|^9.0|^10|^11
- illuminate/database: ^8.0|^9.0|^10|^11
- illuminate/support: ^8.0|^9.0|^10|^11
- nesbot/carbon: ^2|^3
Requires (Dev)
- orchestra/testbench: ^6|^7|^8|^9
README
支持 Laravel 8, 9, 10, 11
简介
Laravel Query Enrich 使得在 Laravel 中创建复杂的数据库查询变得简单,无需编写复杂的 SQL 代码。它简化了开发者与数据库交互的方式,使得在 Laravel 应用程序中构建和阅读查询更为直接。使用 Query Enrich,您可以在无需广泛 SQL 知识的情况下实现高级数据库操作。
使用 Laravel Query Enrich 的好处
无需硬编码查询
无需与复杂的 SQL 作斗争。Laravel Query Enrich 使您的查询更简单。
确保跨数据库兼容性
如果您决定更换数据库引擎,Laravel Query Enrich 可以消除手动代码重构的需要。它处理不同数据库引擎之间的差异,使切换过程平滑,无需程序员进行任何操作!
易于阅读的代码
您的代码变得更加整洁且易于理解。Laravel Query Enrich 使得在 Laravel 应用程序中与数据库交互变得轻松。
示例用法
看看以下示例。它们使用 Laravel Query Enrich 后更加酷炫。不是吗? :)
将书籍分类到不同的价格类别(使用 case when)
使用 Laravel Query Enrich
$books = Book::select( 'id', 'name', QE::case() ->when(c('price'), '>', 100)->then('expensive') ->when(QE::condition(50, '<', c('price')), QE::condition(c('price'), '<=', 100))->then('moderate') ->else('affordable') ->as('price_category') )->get();
不使用 Laravel Query Enrich
$books = Book::select( 'id', 'name', DB::raw(' CASE WHEN price > 100 THEN "expensive" WHEN price BETWEEN 50 AND 100 THEN "moderate" ELSE "affordable" END AS price_category ') )->get();
原始 SQL
select case when `price` > 100 then 'expensive' when (50 < `price` and `price` <= 100) then 'moderate' else 'affordable' end as `price_category` from `books`
获取过去 7 天内下的订单
使用 Laravel Query Enrich
$recentOrders = DB::table('orders') ->where(c('created_at'), '>=', QE::subDate(QE::now(), 7, Unit::DAY)) ->get();
不使用 Laravel Query Enrich
$recentOrders = DB::table('orders') ->whereRaw('created_at >= NOW() - INTERVAL ? DAY', 7) ->get();
原始查询
SELECT * FROM `orders` WHERE `created_at` >= NOW() - INTERVAL 7 DAY;
获取石油和天然气的平均月价格(使用 avg 函数)
使用 Laravel Query Enrich
$monthlyPrices = DB::table('prices') ->select( QE::avg(c('oil'))->as('oil'), QE::avg(c('gas'))->as('gas'), 'month' ) ->groupBy('month') ->get();
不使用 Laravel Query Enrich
$monthlyPrices = DB::table('prices') ->select(DB::raw('avg(`oil`) as `oil`, avg(`gas`) as `gas`, `month`')) ->groupBy('month') ->get();
原始查询
select avg(`oil`) as `oil`, avg(`gas`) as `gas`, `month` from `prices` group by `month`
获取作者并检查他们是否有任何书籍(使用 exists 查询)
使用 Laravel Query Enrich
$authors = DB::table('authors')->select( 'id', 'first_name', 'last_name', QE::exists( Db::table('books')->where('books.author_id', c('authors.id')) )->as('has_book') )->orderBy( 'authors.id' )->get();
不使用 Laravel Query Enrich
$authors = DB::table('authors') ->select( 'id', 'first_name', 'last_name', DB::raw('exists(select * from `books` where `books`.`author_id` = `authors`.`id`) as `has_book`')) ->orderBy( 'authors.id', ) ->get();
原始查询
select `id`, `first_name`, `last_name`, exists(select * from `books` where `books`.`author_id` = `authors`.`id`) as `result` from `authors` order by `authors`.`id` asc
在数据库级别上获取全名(使用 concatws)
使用 Laravel Query Enrich
$authors = Author::select( 'first_name', 'last_name', QE::concatWS(' ', c('first_name'), c('last_name'))->as('result') )->get();
不使用 Laravel Query Enrich
$author = Author::select( 'first_name', 'last_name', DB::raw("concat_ws(' ', `first_name`, `last_name`) as `result`") )->first();
原始查询
select `first_name`, `last_name`, concat_ws(' ', `first_name`, `last_name`) as `result` from `authors`
安装和文档
完整的文档在此处可用
https://laravel-query-enrich.readthedocs.io/
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅许可证文件。
由 Siavash Bamshadnia 用♥编写。
请通过星标此仓库支持我。