PHP code example of dorvidas / laravel-query-builder
1. Go to this page and download the library: Download dorvidas/laravel-query-builder 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/ */
dorvidas / laravel-query-builder example snippets
$users = User::buildFromRequest()
->get();
// all `User`s that with columns `active` equal to 1
$users = User::buildFromRequest()
->get();
// all `User`s with their published `posts` loaded
$users = User::buildFromRequest()
->get();
// all `User`s with their `posts` and post `comments` that are approved
$users = User::buildFromRequest()
->where('active', 1)
->get();
// all `User`s whose name is `John` also enforcing them to be active users
'filters' => [
'eq' => \Dorvidas\QueryBuilder\Filters\EqFilter::class,//Equals
'neq' => \Dorvidas\QueryBuilder\Filters\NotEqFilter::class,//Not equals
'in' => \Dorvidas\QueryBuilder\Filters\InFilter::class,// In
'nin' => \Dorvidas\QueryBuilder\Filters\NotInFilter::class, //Not in
'gt' => \Dorvidas\QueryBuilder\Filters\GreaterThanFilter::class,//Greater than
'gte' => \Dorvidas\QueryBuilder\Filters\GreaterThanEqualFilter::class,//Greater than equals
'lt' => \Dorvidas\QueryBuilder\Filters\LowerThanFilter::class,//Less than
'lte' => \Dorvidas\QueryBuilder\Filters\LowerThanEqualFilter::class,//Less that equals
'like' => \Dorvidas\QueryBuilder\Filters\LikeFilter::class,//Like
'nlike' => \Dorvidas\QueryBuilder\Filters\NotLikeFilter::class,//Not like
'n' => \Dorvidas\QueryBuilder\Filters\NullFilter::class,//Null
'nn' => \Dorvidas\QueryBuilder\Filters\NotNullFilter::class,//Not null
],
use Carbon\Carbon;
use Dorvidas\QueryBuilder\Filters\FilterInterface;
class RecentFilter implements FilterInterface
{
public function apply($query, $value, $params)
{
$col = isset($params[0]) ? $params[0] : 'created_at';
$query->where($col, '>', Carbon::now()->subDay($value)->toDateTimeString());
}
}
$users = User::buildFromRequest()
->get();
// all `User`s with their `posts`
// User model needs to have relation `posts`
//This is fine
$users = User::buildFromRequest((new Constraints())->allowIncludes(['posts']))
->get();
//This is also fine
$users = User::buildFromRequest((new Constraints())->allowIncludes(['posts.comments']))
->get();
// This will throw `IncludeNotAllowedException` exception because no relations allowed
$users = User::buildFromRequest((new Constraints())->allowIncludes([]))
->get();