1. Go to this page and download the library: Download tucker-eric/eloquentfilter 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/ */
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
class UserController extends Controller
{
public function index(Request $request)
{
$query = User::where('company_id', $request->input('company_id'));
if ($request->has('last_name'))
{
$query->where('last_name', 'LIKE', '%' . $request->input('last_name') . '%');
}
if ($request->has('name'))
{
$query->where(function ($q) use ($request)
{
return $q->where('first_name', 'LIKE', $request->input('name') . '%')
->orWhere('last_name', 'LIKE', '%' . $request->input('name') . '%');
});
}
$query->whereHas('roles', function ($q) use ($request)
{
return $q->whereIn('id', $request->input('roles'));
})
->whereHas('clients', function ($q) use ($request)
{
return $q->whereHas('industry_id', $request->input('industry'));
});
return $query->get();
}
}
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
class UserController extends Controller
{
public function index(Request $request)
{
return User::filter($request->all())->get();
}
}
'providers' => [
// Other service providers...
EloquentFilter\ServiceProvider::class,
],
namespace App;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use Filterable;
public function modelFilter()
{
return $this->provideFilter(\App\ModelFilters\CustomFilters\CustomUserFilter::class);
}
//User Class
}
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
use App\ModelFilters\Admin\UserFilter as AdminFilter;
use App\ModelFilters\User\UserFilter as BasicUserFilter;
use Auth;
class UserController extends Controller
{
public function index(Request $request)
{
$userFilter = Auth::user()->isAdmin() ? AdminFilter::class : BasicUserFilter::class;
return User::filter($request->all(), $userFilter)->get();
}
}
use EloquentFilter\ModelFilter;
class UserFilter extends ModelFilter
{
protected $blacklist = ['secretMethod'];
// This will filter 'company_id' OR 'company'
public function company($id)
{
return $this->where('company_id', $id);
}
public function name($name)
{
return $this->where(function($q) use ($name)
{
return $q->where('first_name', 'LIKE', "%$name%")
->orWhere('last_name', 'LIKE', "%$name%");
});
}
public function mobilePhone($phone)
{
return $this->where('mobile_phone', 'LIKE', "$phone%");
}
public function setup()
{
$this->onlyShowDeletedForAdmins();
}
public function onlyShowDeletedForAdmins()
{
if(Auth::user()->isAdmin())
{
$this->withTrashed();
}
}
public function secretMethod($secretParameter)
{
return $this->where('some_column', true);
}
}
public function setup()
{
if(Auth::user()->isAdmin()) {
$this->whitelistMethod('secretMethod');
}
}
namespace App;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use Filterable;
//User Class
}
class UserController extends Controller
{
public function index(Request $request)
{
return User::filter($request->all())->get();
}
}
class User extends Model
{
use Filterable;
public function clients()
{
return $this->hasMany(Client::class);
}
}
class Client extends Model
{
use Filterable;
public function industry()
{
return $this->belongsTo(Industry::class);
}
public function scopeHasRevenue($query)
{
return $query->where('total_revenue', '>', 0);
}
}
class UserFilter extends ModelFilter
{
public function clientsSetup($query)
{
return $query->hasRevenue();
}
}
class UserFilter extends ModelFilter
{
public function industry($id)
{
return $this->related('clients', 'industry_id', '=', $id);
// This would also be shorthand for the same query
// return $this->related('clients', 'industry_id', $id);
}
public function potentialVolume($volume)
{
return $this->related('clients', 'potential_volume', '>=', $volume);
}
}
$this->related('clients', function($query) use ($id) {
return $query->where('industry_id', $id);
});
class UserFilter extends ModelFilter
{
public $relations = [
'clients' => ['industry', 'potential_volume'],
];
}
class ClientFilter extends ModelFilter
{
public $relations = [];
public function industry($id)
{
return $this->where('industry_id', $id);
}
public function potentialVolume($volume)
{
return $this->where('potential_volume', '>=', $volume);
}
}