Laravel检查GET查询的结果是否存在于另一个GET结果中[关闭]

问题描述 投票:1回答:1

我有2个查询:

$branch_workstations = \Workstation::where('branchid', $user->branchid)
                                  ->select('workstationid')
                                  ->get();

$branch_updated = \WorkstationsUpdated::where('branchid', $user->branchid)
                                      ->select('workstation')
                                      ->get();

我想知道分支更新的值是否存在于分支中第一个查询的结果($ branch_workstations)

有帮助吗?

php laravel laravel-5
1个回答
0
投票

你可以使用isEmpty函数来确定它是否有任何结果。

if ($branch_updated ->isEmpty()) {
   ... 
} else {
  $branch_workstations = \Workstation::where('branchid', $user->branchid)
                              ->select('workstationid')
                              ->get();
}

如果您使用的是5.3之前的Laravel版本,则查询构建器将返回一个数组。在这种情况下,您可以使用此数组上的php count函数来了解返回的行数

if (count($branch_updated ) != 0) {

} else {

   $branch_workstations = \Workstation::where('branchid', $user->branchid)
                              ->select('workstationid')
                              ->get();
}
© www.soinside.com 2019 - 2024. All rights reserved.