PHP code example of sbamtr / laravel-query-enrich
1. Go to this page and download the library: Download sbamtr/laravel-query-enrich library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sbamtr / laravel-query-enrich example snippets
$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();
$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();
$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();
$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();
$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();