![](/img/trans.png)
[英]How can I only pluck a column inside the `hasManyThrough` relationship in Laravel?
[英]Laravel 6 pluck in hasManyThrough
控制器:
$files = File::where('agent_id', $user->id)->with('posts')->get();
模型:
public function posts()
{
return $this->hasManyThrough('App\User', 'App\Post', 'id', 'id', 'post_id', 'user_id');
}
所以這會返回一堆數據,例如:
{
"success": [
{
"id": 2,
"post_id": 1,
"transaction_id": 4,
"agent_id": 2,
"status": 0,
"posts": [
{
"id": 1,
"name": "john",
"email": "john@gmail.com",
"phone": "489797878",
"type": "1",
"verified": 1,
"otp": null,
"created_at": "2019-11-23 10:17:31",
"updated_at": "2019-11-23 10:17:51",
"api_token": null,
"laravel_through_key": 1
}
]
}
...
}
我想要的是,排除一些數據,如email
或verified
等。我嘗試了makeHidden
pluck('email')
和makeHidden
但沒有成功。 知道我該怎么做嗎?
做一些像下面這樣的事情怎么樣,(還沒有測試代碼,但應該給你一個線索)
files = File::where('agent_id', $user->id)
->with(['posts' => function ($q) {
$q->select('name','phone'); // specify whatever you want
}])->get(['column1','column2']);
只需使用->get()
包含您想要獲取的列表:
例子:
$files = File::where('agent_id', $user->id)->with('posts')->get(['post_id', 'status']);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.