返回一个Eloquent对象作为JSON动态地隐藏某些列? [英] Dynamically hide certain columns when returning an Eloquent object as JSON?
本文介绍了返回一个Eloquent对象作为JSON动态地隐藏某些列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$ users = User :: all();
return Response :: json($ users);
我知道我可以在模型中设置受保护的属性( $ hidden 或 $ visible ),但是如何动态设置?我可能想在不同的上下文中隐藏或显示不同的列。
解决方案
$ model- > getHidden();
$ model-> setHidden(array $ columns);
$ model-> setVisible(array $ columns);
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.
解决方案
$model->getHidden();
$model->setHidden(array $columns);
$model->setVisible(array $columns);
这篇关于返回一个Eloquent对象作为JSON动态地隐藏某些列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文