2016-12-05 67 views
0

我已阅读了如何temporarily hide model attributes。 我想暂时隐藏模型关系属性。如何用Laravel 5暂时隐藏模型关系属性

例如

{ 
    "slug": "google-chrome", 
    "name": "Google Chrome", 
    "description": { 
     "text": null, 
     "created_at": "2016-12-05 12:16:38", 
     "updated_at": "2016-12-05 12:16:38" 
} 

什么是仅在此查询隐藏description.created_at语法? 在我SoftwareController我有

public function show(Request $request, $slug) 
{ 
    $models = Software::query(); 

    $model = 
     $models 
     ->where('slug', $slug) 
     ->firstOrFail() 
     ->makeHidden([ 
      'description.created_at', 
     ]); 

    return $model; 
} 

这句法似乎不工作?可能吗?

回答

3

makeHidden()不支持点符号。

你应该叫makeHidden上你的相关型号:

$model = $models 
     ->where('slug', $slug) 
     ->firstOrFail(); 

$model->description->makeHidden('created_at'); 

需要注意的是,当你有一个结果,这只会工作。如果你想在一个集合上做到这一点,你必须遍历它并为每个物品运行makeHidden。