如何在 laravel 查询构建器中为同一列构建多个条件的查询?
How to build query for multiple condition for same column in laravel query builder?
我想从 'user' table 获取计数,其中 'plan_validity' 为空或大于今天的日期。这两个条件都需要检查 user_id 和 status = 1.
的同一列
$currentUser = User::where('user_id', $card_details->user_id)
->where('status', 1)
->where(function (Builder $query) {
return $query
->whereDate('plan_validity', '>=', Carbon::now())
->orWhere('plan_validity', null);
})->count();
给出错误
[2021-11-23 10:40:31] production.ERROR: Argument 1 passed to App\Http\Controllers\ProfileController::App\Http\Controllers\{closure}() must be an instance of App\Http\Controllers\Builder, instance of Illuminate\Database\Eloquent\Builder given, called in /home/hellovcard/public_html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php on line 237 {"userId":17,"exception":"[object] (TypeError(code: 0): Argument 1 passed to App\Http\Controllers\ProfileController::App\Http\Controllers\{closure}() must be an instance of App\Http\Controllers\Builder, instance of Illuminate\Database\Eloquent\Builder given, called in /home/hellovcard/public_html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php on line 237 at /home/hellovcard/public_html/app/Http/Controllers/ProfileController.php:34)
如何修改上述查询?
这是我需要得到的输出
enter image description here
您没有正确组合“where”子句。在末尾使用“orWhere”,它基本上忽略了前面 where 子句的所有条件(即它与哪个用户相关),因此将 plan_validity 为空的任何行添加到结果中。因此,在按用户 ID 和状态查询后,您需要将查询的其余部分组合成一个逻辑组,如下所示:
$user = Users::where('user_id', $card_details->user_id)
->where('status', 1)
->where(function (Builder $query) {
return $query
->whereDate('plan_validity', '>=', Carbon::now())
->orWhere('plan_validity', null);
})
->get();
我想从 'user' table 获取计数,其中 'plan_validity' 为空或大于今天的日期。这两个条件都需要检查 user_id 和 status = 1.
的同一列 $currentUser = User::where('user_id', $card_details->user_id)
->where('status', 1)
->where(function (Builder $query) {
return $query
->whereDate('plan_validity', '>=', Carbon::now())
->orWhere('plan_validity', null);
})->count();
给出错误
[2021-11-23 10:40:31] production.ERROR: Argument 1 passed to App\Http\Controllers\ProfileController::App\Http\Controllers\{closure}() must be an instance of App\Http\Controllers\Builder, instance of Illuminate\Database\Eloquent\Builder given, called in /home/hellovcard/public_html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php on line 237 {"userId":17,"exception":"[object] (TypeError(code: 0): Argument 1 passed to App\Http\Controllers\ProfileController::App\Http\Controllers\{closure}() must be an instance of App\Http\Controllers\Builder, instance of Illuminate\Database\Eloquent\Builder given, called in /home/hellovcard/public_html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php on line 237 at /home/hellovcard/public_html/app/Http/Controllers/ProfileController.php:34)
如何修改上述查询?
这是我需要得到的输出
enter image description here
您没有正确组合“where”子句。在末尾使用“orWhere”,它基本上忽略了前面 where 子句的所有条件(即它与哪个用户相关),因此将 plan_validity 为空的任何行添加到结果中。因此,在按用户 ID 和状态查询后,您需要将查询的其余部分组合成一个逻辑组,如下所示:
$user = Users::where('user_id', $card_details->user_id)
->where('status', 1)
->where(function (Builder $query) {
return $query
->whereDate('plan_validity', '>=', Carbon::now())
->orWhere('plan_validity', null);
})
->get();