z-song / laravel-admin

Build a full-featured administrative interface in ten minutes
https://laravel-admin.org
MIT License
11.15k stars 2.82k forks source link

How to add criteria to Selectable #5624

Closed technilogics closed 2 years ago

technilogics commented 2 years ago

Description:

I used selectable to allow users selection, but some users status is inactive, how i can show only Active user in selectable.

`namespace App\Admin\Selectable;

use App\Models\User; use App\Models\UserType; use Encore\Admin\Grid\Filter; use Encore\Admin\Grid\Selectable;

class Users extends Selectable { public $model = User::class;

public function make()
{
    $this->column('id');
    $this->column('name');
    $this->column('user_type_id','User Type')->display(function($id) {
    return ($id ? UserType::find($id)->title_en : null);
    });
    $this->column('email');
    $this->column('avatar','Avatar')->image();
    $this->column('created_at');

    $this->filter(function (Filter $filter) {
        $filter->like('name');
      $filter->equal('user_type_id')->select(UserType::where('status',1)->pluck('title_en', 'id'));
    });
}

} ` i tried as changing

public $model = User::class;

to

public $model = User::where('status',1);

but it failed, as in grid/selectable

protected function initGrid() method

$model = new $this->model(); $this->grid = new Grid(new $model());

is conflicting with my change.

Any help is appreciated, without changing core admin files.

k0hel commented 2 years ago

idk your table, idk I'm right. If it's the same usage as the grid, this should work. $this->model()->where('status', 1);

technilogics commented 2 years ago

Thanks, this works in make() method $this->model()->where('status', 1);