如何在Laravel中隐藏关系列? [英] How to hide relationship columns in laravel?
问题描述
我有一个这样的语句:
App\User::with('client')->find(2)->makeHidden('client.phone_no');
我想从关系中隐藏某些列,但是我不能使用 makeHidden()
,因为它只接受 Model
的参数而不是关系。
I want to hide certain columns from a relation, but I can't do that with makeHidden()
, because it only takes the arguments for the Model
not the relation.
如何隐藏关系中的某些列?
How can I hide some columns from the relation?
推荐答案
如果您不想隐藏 phone_no
通过将所有请求添加到 hidden
属性中,可以执行以下操作:
If you don't want to hide the phone_no
for all the requests by adding it to the hidden
property, you could do something like this:
$user = App\User::with('client')->find(2);
$user->client->makeHidden('phone_no');
return $user;
正如我在对原始问题的评论中所述:我发现 方法。我认为这是您要更频繁地排除列时应使用的方法。如果您只想排除一次列,那么我的解决方案就足够了。
As I stated in my comment to the original question: I found this method as well. I believe this should be the method you should use when you want to exclude columns more often. If you only want to exclude a column once, my solution should be sufficient.
这篇关于如何在Laravel中隐藏关系列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!