Package Data | |
---|---|
Maintainer Username: | xabou |
Maintainer Contact: | bouzopoulos@gmail.com (Harris Bouzopoulos) |
Package Create Date: | 2016-10-14 |
Package Last Update: | 2021-04-29 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-15 15:19:44 |
Package Statistics | |
---|---|
Total Downloads: | 104 |
Monthly Downloads: | 2 |
Daily Downloads: | 0 |
Total Stars: | 1 |
Total Watchers: | 1 |
Total Forks: | 1 |
Total Open Issues: | 1 |
Query is a package that provides queries objects for your Laravel application.
From the command line, run:
composer require xabou/query
Alternatively, you can put the following in your composer.json file:
{
"require": {
"xabou/query": "^0.5.0"
}
}
Open config/app.php
and append providers
array with:
Xabou\Query\QueryServiceProvider::class
With the package now installed, a new artisan command is available.
php artisan make:query PopularUsersQuery
A 'Queries' directory will be created, if it doesn't already exist, within your app.
Within the body method you can declare your query:
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');
}
This is where your query logic should be declared. This method returns an instance of Eloquent Database Builder or executed query.
This is a wrapper for body method. When body returns an instance of Eloquent Database Builder this method delegates to Builder's get method.
You can call any method defined in Eloquent Database Builder by returning an instance of it, like above example.
// Dynamic static method calls
PopularUsersQuery::first()
// or
PopularUsersQuery::get()
// Dynamic method calls
(new PopularUsersQuery())->get()
Within the body method you can also execute your query.
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.
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.
By returning the content of body you can continue chaining methods on Eloquent Database Builder.
PopularUsersQuery::body()->where('age', '>', 25)->get();
You can pass as many arguments as you want through body or get methods.
$age = 25;
$verified = 1
PopularUsersQuery::get($age, $verified);
public static function body($age, $verified)
{
return User::where('age', '>', $age)
->where('verified', $verified);
}