Laravel查询检查表列值是否存在

时间:2019-11-22 13:19:28

标签: php laravel

我有一个Category表,在我的RegistrationController中,我在另一个查询中使用了Category,所以我希望另一个查询检查cte_gender_id列中的值是否为1、2和3。

我现在所拥有的:(在RegistrationController中)

$category = Category::where('cte_distance_id', $distance_id)
  ->where('cte_gender_id', $gender_id);
if(is_null($categorie)){
  return back()->with("errorMessage", "Categorie not found");
}

感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

尝试这种方式:

$gender_id = ['1', '2', '3'];

// If you suppose collection
$category = Category::where('cte_distance_id', $distance_id)
                    ->whereIn('cte_gender_id', $gender_id)->get();

if($category->isEmpty()){
    return back()->with("errorMessage", "Categorie not found");
}

// If you suppose single element
$category = Category::where('cte_distance_id', $distance_id)
                        ->whereIn('cte_gender_id', $gender_id)->first();

if($category){
    return back()->with("errorMessage", "Categorie not found");
}

答案 1 :(得分:0)

您可以通过这种方式对get和first方法进行评估:

torch.Size([40500, 784])