簡體   English   中英

Laravel 6 使用 hasManyThrough

[英]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
                }
            ]
        }
...
    }

我想要的是,排除一些數據,如emailverified等。我嘗試了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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM