2016-12-05 64 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。