MedicOneSystems / livewire-datatables

Advanced datatables using Laravel, Livewire, Tailwind CSS and Alpine JS
https://livewire-datatables.com/
MIT License
1.19k stars 259 forks source link

attributes/appends #58

Closed rodrigocardenas closed 3 years ago

rodrigocardenas commented 4 years ago

Hi, any way how to use attributes/appends from models? btw nice work thx!

vandadsoft commented 4 years ago

Any Solution?

carlituxman commented 3 years ago

I've do this:

Column::callback(['id'], function ($id) {
                return MyModel::find($id)->my_attribute;
            })->label(__('MyName')),
amaurycid commented 3 years ago

For now, you should define accesors through callbacks. Seems this package use fluent queries in its inner workings, not Eloquent (that's why model accessors doesn't work).

mahmoud-m-abadi commented 3 years ago

Hi @carlituxman s solution is not good for huge data in table.

@marksalmon do you have another solution or trick about that ?

marksalmon commented 3 years ago

Livewire Datatables uses query builder and SQL directly because hydrating eloquent models is not permormant with large datasets. Therefore you do have to redefine your accessors either in an SQL query using Column::raw() or in a callback method.

shahidkarimi commented 1 year ago

@marksalmon the callback should have access to the current row. like

Column::callback(function($row){
  return $row->custom_attribute;
})

What's wrong implemting this?

Remember we are always doing: ModelClass::query()in the builder method.

So it does not make sense not be able to access computed attributes.