Is there any way in Laravel 5 to check if some value equal with value from database?
Here is what I try to do: I have table users and in table users I have additional column admin_id. In validation I need to check if admin_id form database equal to 1.
Here is my current code:
$inputs = array(
'projects' => Input::get('project'),
'users' => Input::get('workers')
);
$rules = array(
'projects' => 'required',
'users' => 'required'
);
$validator = Validator::make($inputs,$rules);
if($validator->fails()){
return false;
}else{
return true;
}
I don't know what is users input here - is it if from users table? If yes, you can then create your rules this way:
$rules = array(
'projects' => 'required',
'users' => ['required', 'exists:users,id,admin_id,1']
);
So now it will be verified if users matches user_id from users table where admin_id equals 1.
You should also consider Laravel 5 Requests objects to validate input. It's much more clean than putting code in Controller/Model/Repository. More about Requst Validation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With