Let's get started quickly We start with WhereRaw
$products = DB::table('products')
->whereRaw('price > IF(state = "TX", ?, 100)', [200])
->get();
havingRaw
Product::groupBy('product_id')->havingRaw('COUNT(*) > 1')->get();
orderByRaw
User::where('created_at', '>', '2022-01-01')
->orderByRaw('(updated_at - created_at) desc')
->get();
Eloquent selectRaw()
User::select("*")
->selectRaw('amount + ? as amount_with_bonus', [500])
->get();
Another example
User::select("*")
->select('*', DB::raw('amount + 500 as amount_with_bonus'))
->get();
Eloquent whereNotNull()
select * from `users` where `email_verified_at` is not null
User::select("*")
->whereNotNull('email_verified_at')
->get();
Eloquent whereNull()
select * from `users` where `email_verified_at` is null
User::select("*")
->whereNull('email_verified_at')
->get();
I hope you enjoyed the code.
Top comments (0)