I am adding scope on my base query and I'm stuck on It.
Everything is working fine but I don't get It how to add scope on my query e,g If I select a gender from the dropdown then an Ajax request should be sent like
$query->where('gender','Male');
Next time I want to add a Filter on currently filtered data of genders and for this purpose I again addScope() on filtered data
$query->where('age','>',20);
Then the result should be all those users whose gender is Male and age is greater than 20 should be populated in DataTable.
Please tell me how to do this
@yajra @wuwx @MarkVaughn @ligne13 @donnykurnia @tortuetorche @ethaizone @mtvbrianking @alfa6661 and Others, Please tell me how to do this?
Here is an example:
php artisan datatables:scope AdminUsersScope
// AdminUsersScope.php
namespace App\DataTables\Scopes;
use Yajra\DataTables\Contracts\DataTableScope;
class AdminUsersScope implements DataTableScope
{
/**
* Apply a query scope.
*
* @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query
* @return mixed
*/
public function apply($query)
{
return $query->where('is_admin', true);
}
}
On your users controller:
class UsersController extends DTEController
{
/**
* @param UsersDataTable $dataTable
* @return mixed
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function index(UsersDataTable $dataTable)
{
return $dataTable->addScope(new AdminUsersScope)->render('admin::users.index');
}
}
@yajra I did the same but when I return response to json It gives me Html as well due to render function. but I just want to update table without page refresh on ajax call
@yajra Can you please tell me how should I update my existing datatable.
Now I am getting exact response of Json but When I draw() datatable It calls draw for 2 times, 1 time for my json request and then it again run draw() on all data and again filter data is not displaying
Here is an example:
php artisan datatables:scope AdminUsersScope // AdminUsersScope.php namespace App\DataTables\Scopes; use Yajra\DataTables\Contracts\DataTableScope; class AdminUsersScope implements DataTableScope { /** * Apply a query scope. * * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query * @return mixed */ public function apply($query) { return $query->where('is_admin', true); } }On your users controller:
class UsersController extends DTEController { /** * @param UsersDataTable $dataTable * @return mixed * @throws \Illuminate\Auth\Access\AuthorizationException */ public function index(UsersDataTable $dataTable) { return $dataTable->addScope(new AdminUsersScope)->render('admin::users.index'); } }
hello, you know why I get the error Call to undefined method addScope () when I try to use it in my controller just like in your example
Would be nice to see some proper documentation. With an entire controller using ajax AND scopes. This service documentation is very hard to understand due to all the missing parts