Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically hide certain columns when returning an Eloquent object as JSON?

How do dynamically hide certain columns when returning an Eloquent object as JSON? E.g. to hide the 'password' column:

$users = User::all(); return Response::json($users); 

I'm aware I can set protected properties in the model ($hidden or $visible), but how do I set these dynamically? I might want to hide or show different columns in different contexts.

like image 477
mtmacdonald Avatar asked Jul 15 '14 12:07

mtmacdonald


1 Answers

$model->getHidden(); $model->setHidden(array $columns);  $model->setVisible(array $columns); 
like image 104
Jarek Tkaczyk Avatar answered Sep 28 '22 03:09

Jarek Tkaczyk