larabel で
contact モデルから
hasmany contactsubs テーブルで、contactsubs を 2つ以下で
no_from_id は 持っていないレコードを取得したい。
・OK
$sql = Contact::has('contactsubs','<',2); $sql->with('contactsubs.user.photos'); $res = $sql->paginate(5);
・OK
$request->no_from_id = 37; $sql = Contact::wheredoesntHave('contactsubs', function ($query) use ($no_from_id) { $query->where('from_id', $no_from_id); }); $sql->with('contactsubs.user.photos'); $res = $sql->paginate(5);
・NG
$sql = Contact::has('contactsubs','<',2); $sql = $sql->wheredoesntHave('contactsubs', function ($query) use ($no_from_id) { $query->where('from_id', $no_from_id); }); $sql->with('contactsubs.user.photos'); $res = $sql->paginate(5);
どうすれば、 has と wheredoesnthaveを両方動かせますか?
回答1件
あなたの回答
tips
プレビュー