Excluding Laravel appends values from model results
Excluding Laravel appends values from model results
Laravel has the option to add an $appends array to each model making additional values automatically available as if they are database attributes by adding accessors for each.
$appends
This is normally pretty handy, except in this case I need to ONLY get the fields I put into select() because DataTables is expecting only what I send to it.
select()
Example:
Item::select(['image', 'name', 'color']);
Will return appended fields after color in the attributes.
color
How do I force the exclusion of the appends values when returning results?
Or alternatively, how do I get DataTables to ignore certain attributes?
Not sure which is the least time costly route.
Currently using yajra/laravel-datatables package to send data to the jQuery DataTables AJAX request.
yajra/laravel-datatables
2 Answers
2
You can call each function in the collection object and then use setHidden method to exclude the unwanted fields like this
$item= Item::select(['image', 'name', 'color'])->get()->each(function($row)
$row->setHidden(['appendedField1', 'appendedField2']);
);
And for the yajra/laravel-datatables you can use something like
$item= Item::select(['image', 'name', 'color']);
return Datatables::of($item)->remove_column('appendedField1');
get()
yajra/laravel-datatables
I think you can use remove_column('columnName') method in yajra/laravel-datatables package.
– Milan Maharjan
Oct 30 '15 at 9:08
To solve this I made
$appends public on my Item model, created var $DT = Datatables::of($items); and then used call_user_func_array([$DT, 'removeColumn'], $items->first()->appends); before returning make(). Has to be called this way with yajra/laravel-datatables-oracle v3 if passing an array.– eComEvo
Oct 30 '15 at 14:43
$appends
Item
$DT = Datatables::of($items);
call_user_func_array([$DT, 'removeColumn'], $items->first()->appends);
make()
yajra/laravel-datatables-oracle
Correction, can't do it that way or it messes up pagination. Had to create static method for it which I've posted in a solution. +1 your answer for helping me figure out full solution.
– eComEvo
Oct 30 '15 at 16:26
To solve this I added this method to my Item model:
Item
public static function getAppends()
$vars = get_class_vars(__CLASS__);
return $vars['appends'];
Then used the following code in the controller:
$items = Item::select(['image', 'name', 'color']);
$DT = Datatables::of($items);
call_user_func_array([$DT, 'removeColumn'], Item::getAppends()); // Has to be called this way with yajra/laravel-datatables-oracle v3.* if passing an array.
return $DT->make(true);
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Unfortunately I can't use
get()on the results before passing toyajra/laravel-datatablespackage which passes it to the jQuery DataTables package over AJAX. Forgot to mention that package. Otherwise this would be a perfect solution!– eComEvo
Oct 29 '15 at 22:36