Laravel validation rule 'Exists' with secret feature.
In this post, I wish to share one secret feature of laravel validation rule 'Exists'. If you have worked with laravel and familiar with laravel validation, you must know what does 'Exists' rule checks.
If not let me give here a brief about this. In simple words, 'Exists' rule check if a record exists in the given database table with column value matching with the provided input value.
Format:
Let's take an example.
Have you thought about what if you have to check if records exist or not based on multiple columns (more than one column)?
Like what if we need to check if the user exists in users table against given user id with role type as student and status as active?
Now let me show you the secret. Here you need to check the records with 3 conditions. So you can use exists rule here as below:
Let me explain how? First 2 parameters passed to exists rule works as normal. For extra conditions, we just need to give column, value pair after the 2 basic parameters.
like here: role,student i.e role = student and status,active i.e. status = active.
What laravel code does here, extra parameters passed to the rule are considered as column name => value pairs and append to the where array like in eloquent we use:
If not let me give here a brief about this. In simple words, 'Exists' rule check if a record exists in the given database table with column value matching with the provided input value.
Format:
'exists:table,column'So this is what as mentioned in laravel documentation too. So looking at the format we understand that this rule is to validate given input value by check if any records exist or not for that particular input value matching with the given column values of the table.
Let's take an example.
'booking_id' => 'exists:bookings,id'Here we are validating provided booking_id with exists, if records exist in bookings table matching the column id (in most of the cases but can use any column as per your requirement.)
Have you thought about what if you have to check if records exist or not based on multiple columns (more than one column)?
Like what if we need to check if the user exists in users table against given user id with role type as student and status as active?
Now let me show you the secret. Here you need to check the records with 3 conditions. So you can use exists rule here as below:
'user_id' => 'exists:users,id,role,student,status,active';Yes! it will work.
Let me explain how? First 2 parameters passed to exists rule works as normal. For extra conditions, we just need to give column, value pair after the 2 basic parameters.
like here: role,student i.e role = student and status,active i.e. status = active.
What laravel code does here, extra parameters passed to the rule are considered as column name => value pairs and append to the where array like in eloquent we use:
->where(['role'=>'student', 'status'=>'active']);Now, I hope you will get the idea of how to use laravel validation rule 'exists' as per your need. Feel free for your queries or feedback.
Comments
Post a Comment