Supports Laravel 8, 9, 10, 11
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.
You don't have to struggle with complicated SQL. Laravel Query Enrich makes your queries simpler.
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!
Your code becomes cleaner and easier to understand. Laravel Query Enrich makes interacting with databases in your Laravel applications a breeze.
Look at the following examples. They are way cooler with the Laravel Query Enrich. Aren't they? :)
$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();
$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();
select case
when `price` > 100 then 'expensive'
when (50 < `price` and `price` <= 100) then 'moderate'
else 'affordable' end as `price_category`
from `books`
$recentOrders = DB::table('orders')
->where(c('created_at'), '>=', QE::subDate(QE::now(), 7, Unit::DAY))
->get();
$recentOrders = DB::table('orders')
->whereRaw('created_at >= NOW() - INTERVAL ? DAY', 7)
->get();
SELECT *
FROM `orders`
WHERE `created_at` >= NOW() - INTERVAL 7 DAY;
$monthlyPrices = DB::table('prices')
->select(
QE::avg(c('oil'))->as('oil'),
QE::avg(c('gas'))->as('gas'),
'month'
)
->groupBy('month')
->get();
$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`
$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();
$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
$authors = Author::select(
'first_name',
'last_name',
QE::concatWS(' ', c('first_name'), c('last_name'))->as('result')
)->get();
$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`
The complete documentation is available here:
https://laravel-query-enrich.readthedocs.io/
The MIT License (MIT). Please see the License file for more information.
Written with ♥ by Siavash Bamshadnia.
Please support me by staring this repository.