Abandoned
All good things come to an end.
Thanks everyone for using this package :), (*1)
Laravel Query
Query is a package that provides queries objects for your Laravel application., (*2)
Installation
1: Require via composer
From the command line, run:, (*3)
composer require xabou/query
Alternatively, you can put the following in your composer.json file:, (*4)
{
"require": {
"xabou/query": "^0.5.0"
}
}
2: Register Service Provider
Open config/app.php and append providers array with:, (*5)
Xabou\Query\QueryServiceProvider::class
Usage
Create A Query
With the package now installed, a new artisan command is available., (*6)
php artisan make:query PopularUsersQuery
A 'Queries' directory will be created, if it doesn't already exist, within your app., (*7)
Declare Query Body
Within the body method you can declare your query:, (*8)
public static function body()
{
return User::select(['user.username', 'user.id', 'user.verified', 'popularity_user.score'])
->join('popularity_user', 'users.id', '=', 'popularity_user.user_id')
->where('user.verified', 1)
->with('avatar')
->orderBy('popularity_user.score', 'DESC')
->orderBy('user.username', 'ASC');
}
Fetching Results
Body
This is where your query logic should be declared.
This method returns an instance of Eloquent Database Builder or executed query., (*9)
Get
This is a wrapper for body method. When body returns an instance of Eloquent Database Builder
this method delegates to Builder's get method., (*10)
1: Delegate to Eloquent Database Builder
You can call any method defined in Eloquent Database Builder by returning an instance of it, like above example., (*11)
// Dynamic static method calls
PopularUsersQuery::first()
// or
PopularUsersQuery::get()
// Dynamic method calls
(new PopularUsersQuery())->get()
2: Execute query
Within the body method you can also execute your query., (*12)
public static function body()
{
return User::select(['user.username', 'user.id', 'user.verified', 'popularity_user.score'])
->join('popularity_user', 'users.id', '=', 'popularity_user.user_id')
->where('user.verified', 1)
->with('avatar')
->orderBy('popularity_user.score', 'DESC')
->paginate();
}
Then simply call body or get method on Query., (*13)
PopularUsersQuery::body()
//or
PopularUsersQuery::get()
Note: In this case get method serves as an alias to body method. It won't delegate to Eloquent Database Builder., (*14)
3: Chain query
By returning the content of body you can continue chaining methods on Eloquent Database Builder., (*15)
PopularUsersQuery::body()->where('age', '>', 25)->get();
Passing Arguments
You can pass as many arguments as you want through body or get methods., (*16)
$age = 25;
$verified = 1
PopularUsersQuery::get($age, $verified);
public static function body($age, $verified)
{
return User::where('age', '>', $age)
->where('verified', $verified);
}