亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel 根據關系結果返回數據

Laravel 根據關系結果返回數據

PHP
狐的傳說 2022-06-17 14:28:36
PostController.php$customers = $query->orderBy('id', 'desc')->with('agents:agents.id')->get();        foreach ($customers as $customer) {            $customer->calculator = $this->calculator($customer);        }        return response()->json($customers, 200 );Post.phpfunction agents()    {        return $this->belongsToMany('App\Agent','files','post_id','agent_id');    }這將返回正確的數據,如下所示:[{    "id": 5,    "hash": "SqB29tkfm1dwGsXp4ZCV",    "agents": [{      "id": 1,      "pivot": {        "post_id": 5,        "agent_id": 1      }    }]  },  {    "id": 4,    "hash": "SqH29tkfm1dwGsXp4ZCV",    "agents": []  },  {    "id": 3,    "hash": "RqH29tkfm1dwGsXp4ZCV",    "agents": [{      "id": 1,      "pivot": {        "post_id": 3,        "agent_id": 1      }    }]  },  {    "id": 1,    "hash": "RqH29tkfm1dwGrXp4ZCV",    "agents": [{      "id": 1,      "pivot": {        "post_id": 1,        "agent_id": 1      }    }]  }]但我想要的是只返回那些有空代理的數據[],在這種情況下,應該只返回 id4因為代理數組是空的。我怎樣才能做到這一點?
查看完整描述

2 回答

?
紅糖糍粑

TA貢獻1815條經驗 獲得超6個贊

嘗試這個 :

$customers = $query->orderBy('id', 'desc')->whereDoesntHave('agents')->get();

如果你想得到沒有代理的客戶。在這里閱讀更多


查看完整回答
反對 回復 2022-06-17
?
臨摹微笑

TA貢獻1982條經驗 獲得超2個贊

你可以同時使用 dontHave 或 whereDoesntHave like

$customers = $query->orderBy('id', 'desc')->doesntHave('agents')->get();

或者

$customers = $query->orderBy('id', 'desc')->whereDoesntHave('agents')->get();


查看完整回答
反對 回復 2022-06-17
  • 2 回答
  • 0 關注
  • 182 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號