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');






Unfortunately I can't use get() on the results before passing to yajra/laravel-datatables package 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


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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

Node.js puppeteer - Use values from array in a loop to cycle through pages