kdion4891 / laravel-livewire-tables

A dynamic, responsive Laravel Livewire table component with searching, sorting, checkboxes, and pagination.
302 stars 41 forks source link

Example for restricting the results #37

Open merlinro opened 4 years ago

merlinro commented 4 years ago

It has taken me more time than it should... I only wanted users with the role_id = 1

I now have a solution and I wanted to share it.

class UserTable extends TableComponent { public $role_id; public function query() { if ($this->role_id!= null) return User::where('role_id',$this->role_id); return User::query(); } public function mount($role_id= null) { $this->setTableProperties(); if ($role_id!= null) $this->role_id= $role_id; } public function columns() { return [ Column::make('ID')->searchable()->sortable(), Column::make('url')->searchable()->sortable(), Column::make('status')->searchable()->sortable(), Column::make('type')->searchable()->sortable(), Column::make('role')->searchable()->sortable(), Column::make('processed_at')->searchable()->sortable(), Column::make('Created At')->searchable()->sortable(), Column::make('Updated At')->searchable()->sortable(), ]; } }

and in the view:

@livewire('user-table',['role_id'=>$role->id])

I hope I could help