You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was getting frustrated by wanting columns formatted in a particular way, adding an accessor to the model, adding the accessor to the $appends of the model, then putting the accessor name in the Column:make()
Then after doing all this, invariably, the search and sort functions would be broken because livewire tables tries to use the accessor in the model search and sort by, resulting in unknown column (name of accessor)
The below seems to be a simple way to deal with this, and in keeping with the package;
Having published the views; In the laravel-livewire-tables/table.blade.php;
each cell will now call the tdPresenter function in the component, passing the column name and the value
In the component, add a method, like;
public function tdPresenter($attribute, $value)
{
if ($attribute == 'budget') return '£' . round($value, 2);
if ($attribute == 'created_at') return Carbon::parse($value)->format('H:i D d.m.y');
if ($attribute == 'updated_at') return Carbon::parse($value)->format('H:i D d.m.y');
return $value;
}
Now I can format dates and currencies in the component, I don't have to pollute my models, and sorting and searching still works on these columns.
The text was updated successfully, but these errors were encountered:
I was getting frustrated by wanting columns formatted in a particular way, adding an accessor to the model, adding the accessor to the
$appends
of the model, then putting the accessor name in theColumn:make()
Then after doing all this, invariably, the search and sort functions would be broken because livewire tables tries to use the accessor in the model search and sort by, resulting in unknown column (name of accessor)
The below seems to be a simple way to deal with this, and in keeping with the package;
Having published the views; In the laravel-livewire-tables/table.blade.php;
change
to
each cell will now call the tdPresenter function in the component, passing the column name and the value
In the component, add a method, like;
Now I can format dates and currencies in the component, I don't have to pollute my models, and sorting and searching still works on these columns.
The text was updated successfully, but these errors were encountered: