Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel查询检查表列值是否存在_Php_Laravel - Fatal编程技术网

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

Php Laravel查询检查表列值是否存在,php,laravel,Php,Laravel,我有一个类别表,在我的RegistrationController中,我在另一个查询中使用该类别,因此我希望另一个查询检查cte_gender_id在列中是否有1、2和3作为值 我现在拥有的:(在注册控制器中) 谢谢你的帮助 试着这样做: $gender_id = ['1', '2', '3']; // If you suppose collection $category = Category::where('cte_distance_id', $distance_id)

我有一个类别表,在我的RegistrationController中,我在另一个查询中使用该类别,因此我希望另一个查询检查cte_gender_id在列中是否有1、2和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");
}

对于get和first方法,都可以使用这种方法进行评估:

if(!isset($category) || count($category) < 1){
    return back()->with("errorMessage", "Categorie not found");
}
if(!isset($category)| count($category)<1){
return back()->带有(“errorMessage”,“未找到分类”);
}

嘿,谢谢你的快速回答!我试过了,分类中的cte_性别id中没有“3”,但它仍然没有给出错误。我尝试了第一组代码我只需要检查cte_gender_id,如果这是你问的问题你希望这个查询会给你一个元素或元素数组吗?哦,单元素在我的代码中使用这个用例://如果你假设单元素
if(!isset($category) || count($category) < 1){
    return back()->with("errorMessage", "Categorie not found");
}