chastephp / laravel-wheres
A laravel wheres. Inspired by medoo
3.0.0
2022-05-07 03:30 UTC
Requires
- php: ^7.1.3|^8.0
- illuminate/database: ^6.9|^7.0|^8.0|^9.0
Requires (Dev)
- orchestra/testbench: ^4.0|^5.0|^6.0|^7.0
- phpunit/phpunit: ^8.0|^9.3
README
A laravel wheres. Inspired by medoo
Requirements
- laravel >=5.5
Installation
composer require chastephp/laravel-wheres
Usage
The SQL in the comments is based on MySQL
User::query()->wheres([ "email" => "foo@bar.com" ])->toSql(); // select * from `users` where `email` = ? User::query()->wheres([ "user_id[>]" => 200 ])->toSql(); // select * from `users` where `user_id` > ? User::query()->wheres([ "user_id[>=]" => 200 ])->toSql(); // select * from `users` where `user_id` >= ? User::query()->wheres([ "user_id[!]" => 200, ])->toSql(); // select * from `users` where `user_id` != ? User::query()->wheres([ "age[<>]" => [20, 50] ])->toSql(); // select * from `users` where `age` between ? and ? User::query()->wheres([ "birthday[<>]" => [date("Y-m-d", strtotime('-30 days')), date("Y-m-d")] ])->toSql(); // select * from `users` where `birthday` between ? and ? User::query()->wheres([ "birthday[><]" => [date("Y-m-d", strtotime('-30 days')), date("Y-m-d")] ])->toSql(); // select * from `users` where `birthday` not between ? and ? User::query()->wheres([ "OR" => [ "user_id" => [2, 123], "email" => ["foo@bar.com", "cat@dog.com", "admin@medoo.in"] ] ])->toSql(); // select * from `users` where (`user_id` in (?, ?) or `email` in (?, ?, ?)) User::query()->wheres([ "AND" => [ "user_name[!]" => "foo", "user_id[!]" => 1024, "email[!]" => ["foo@bar.com", "cat@dog.com", "admin@medoo.in"], "city[!]" => null, "promoted[!]" => true ] ])->toSql(); // select * from `users` where (`user_name` != ? and `user_id` != ? and `email` not in (?, ?, ?) and `city` is not null and `promoted` != ?)
Compound
User::query()->wheres([ "AND" => [ "OR" => [ "user_name" => "foo", "email" => "foo@bar.com" ], "password" => "12345" ] ])->toSql(); // select * from `users` where ((`user_name` = ? or `email` = ?) and `password` = ?) User::query()->wheres([ "AND #1" => [ "OR #the first condition" => [ "user_name" => "foo", "email" => "foo@bar.com" ], "OR #the second condition" => [ "user_name" => "bar", "email" => "bar@foo.com" ] ] ])->toSql(); // select * from `users` where ((`user_name` = ? or `email` = ?) and (`user_name` = ? or `email` = ?)) User::query()->wheres([ "nickname[~]#1" => '%foo%', "nickname[~]#2" => '%bar%', ])->toSql(); // select * from `users` where `nickname` like ? and `nickname` like ? User::query()->wheres([ 'OR' => [ "nickname[~]#1" => '%foo%', "nickname[~]#2" => '%bar%', ] ])->toSql(); // select * from `users` where (`nickname` like ? or `nickname` like ?)
LIKE
User::query()->wheres([ "city[~]" => "%stan%" ])->toSql(); // select * from `users` where `city` like ? User::query()->wheres([ "city[!~]" => "%stan%" ])->toSql(); // select * from `users` where `city` not like ?
Quick And/Or
User::query()->wheres([ "province|city[~]" => "%stan%" ])->toSql(); // select * from `users` where (`province` like ? or `city` like ?) User::query()->wheres([ "province&city[~]" => "%stan%" ])->toSql(); // select * from `users` where (`province` like ? and `city` like ?)