I have a Category table and in my RegistrationController i use the Category in another query, so i want another Query to check if cte_gender_id has 1, 2, and 3 as a value in the column.
What i have right now:(in the 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");
}
Thanks for the help!
Try this way:
$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");
}
You can use whereIn
method of Elequent ORM
to check with Array values,
For Mutiple Data
$gender_id_list = ['1', '2', '3'];
$category = Category::where('cte_distance_id', $distance_id)
->whereIn('cte_gender_id', $gender_id_list)->get();
if($category->isEmpty()){
return back()->with("errorMessage", "Categorie not found");
}
For Single and First Record
$gender_id_list = ['1', '2', '3'];
$category = Category::where('cte_distance_id', $distance_id)
->whereIn('cte_gender_id', $gender_id_list)->first();
if(!$category){
return back()->with("errorMessage", "Categorie not found");
}
you can evaluate this way for both get and first methods:
if(!isset($category) || count($category) < 1){
return back()->with("errorMessage", "Categorie not found");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.