sbamtr/laravel-query-enrich
Composer 安装命令:
composer require sbamtr/laravel-query-enrich
包简介
A helper for Laravel eloquent and query builder
README 文档
README
Supports Laravel 8, 9, 10, 11, 12
Introduction
Laravel Query Enrich makes it easy to create complex database queries in Laravel without having to write complicated SQL code. It simplifies the way developers interact with databases, making it more straightforward to build and read queries in Laravel applications. With Query Enrich, you can achieve advanced database operations without the need for extensive SQL knowledge.
Benefits of Using Laravel Query Enrich:
No Hard Query Code
You don't have to struggle with complicated SQL. Laravel Query Enrich makes your queries simpler.
Ensures Cross-Database Compatibility
If you decide to switch the database engine, Laravel Query Enrich eliminates the need for manual code refactoring. It handles the difference between different database engines and makes things switch smoothly, all without needing programmers to do anything!
Easy-to-Read Code
Your code becomes cleaner and easier to understand. Laravel Query Enrich makes interacting with databases in your Laravel applications a breeze.
Example Usage
Look at the following examples. They are way cooler with the Laravel Query Enrich. Aren't they? :)
Categorize books into different price categories (using case when)
With 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();
Without 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();
Raw SQL
select case when `price` > 100 then 'expensive' when (50 < `price` and `price` <= 100) then 'moderate' else 'affordable' end as `price_category` from `books`
Fetch orders placed in the last 7 days
With Laravel Query Enrich
$recentOrders = DB::table('orders') ->where(c('created_at'), '>=', QE::subDate(QE::now(), 7, Unit::DAY)) ->get();
Without Laravel Query Enrich
$recentOrders = DB::table('orders') ->whereRaw('created_at >= NOW() - INTERVAL ? DAY', 7) ->get();
Raw Query
SELECT * FROM `orders` WHERE `created_at` >= NOW() - INTERVAL 7 DAY;
Get average monthly price for oil and gas (using avg function)
With Laravel Query Enrich
$monthlyPrices = DB::table('prices') ->select( QE::avg(c('oil'))->as('oil'), QE::avg(c('gas'))->as('gas'), 'month' ) ->groupBy('month') ->get();
Without Laravel Query Enrich
$monthlyPrices = DB::table('prices') ->select(DB::raw('avg(`oil`) as `oil`, avg(`gas`) as `gas`, `month`')) ->groupBy('month') ->get();
Raw Query
select avg(`oil`) as `oil`, avg(`gas`) as `gas`, `month` from `prices` group by `month`
Fetch authors and check if they have any books (using exists query)
With 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();
Without 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();
Raw Query
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
Getting full name on database level (using concatws)
With Laravel Query Enrich
$authors = Author::select( 'first_name', 'last_name', QE::concatWS(' ', c('first_name'), c('last_name'))->as('result') )->get();
Without Laravel Query Enrich
$author = Author::select( 'first_name', 'last_name', DB::raw("concat_ws(' ', `first_name`, `last_name`) as `result`") )->first();
Raw Query
select `first_name`, `last_name`, concat_ws(' ', `first_name`, `last_name`) as `result` from `authors`
Installation and documentation
The complete documentation is available here:
https://laravel-query-enrich.readthedocs.io/
License
The MIT License (MIT). Please see the License file for more information.
Written with ♥ by Siavash Bamshadnia.
Please support me by staring this repository.
sbamtr/laravel-query-enrich 适用场景与选型建议
sbamtr/laravel-query-enrich 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20.4k 次下载、GitHub Stars 达 140, 最近一次更新时间为 2024 年 01 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 sbamtr/laravel-query-enrich 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sbamtr/laravel-query-enrich 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 20.4k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 140
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-01-24