laravellaravel-livewirelaravel-datatables

Livewire Datatables - Transform column and filter


I'm trying to substitute column data through callback, that has to return a username, mapped to user unique identifier. The list of usernames is outside of of the model' data and that's where I'm stuck. I cannot use filterable() with the list of usernames because it will be incorrect since Eloquent won't be able to filter by username - only by user identifier.

The final result of below code is a query which does 'WHERE users.id = The username for X' instead of 'WHERE users.id = X'.

My model:

<?php

class User extends Model 
{
    protected $table = 'users';
    protected $fillable = ['id'];

    public static function getNamesProperty()
    {
         return ['Username #1', 'Username #2'];
    }
}


class LogsTable extends LivewireDatatable
{
    public $model      = User::class;
    public $exportable = true;
    public $hideable   = 'select';

    public function builder()
    {
        return User::query();
    }

    public function columns()
    {
        return [
        Column::callback(['id'], function ($id) {
            return "The username for " . $id;
        })->label('Username')->filterOn('users.id')->filterable(User::names()),
         ];
    }

The question is how do I achieve the goal - be able to filter, while displaying username instead of identifier?


Solution

  • There is an answer on GitHub : https://github.com/MedicOneSystems/livewire-datatables/issues/284

    Thank you Mark!