Laravel雄辩的等同于MYSQL IN

时间:2018-06-20 07:11:00

标签: mysql laravel eloquent

我有两个桌子。我想获取所有处于活动状态/被禁止的用户及其详细信息。

laravel雄辩地等同于此查询。

  'select * from client_details where user_id IN (select user_id from client_logins where is_active='.$active.')'

表: client_logins 模型clientLogin

+---------+---------+--------------+-------------+-----------+
| id      | user_id |  username    | password    | is_active |
+---------+---------+--------------+-------------+-----------+

表: client_details 模型clientDetail

+---------+------+-------+------------------+
| user_id | name | email | mobile |  address|
+---------+------+-------+------------------+

1 个答案:

答案 0 :(得分:1)

使用查询生成器,您可以将其编写为

const input1 = [2, 3, 1, 5];
const input2 = [2, 3, 1, 5, 4, 6, 7, 9, 10];
const input3 = [3, 4, 5, 6, 8];

function findMissing(arr) {
  const min = Math.min(...arr);
  const set = new Set(arr);
  return Array.from(
    { length: set.size },
    (_, i) => i + min
  ).find(numToFind => !set.has(numToFind));
}
console.log(findMissing(input1));
console.log(findMissing(input2));
console.log(findMissing(input3));

或者,如果为$clients=DB::table('client_details as c') ->select('c.*') ->join('client_logins as l', 'c.user_id', '=', 'l.user_id') ->where('l.is_active', '=', $active) ->get() ; 模型定义了has关系,则可以使用ClientDetails过滤器

whereHas