You can create custom validation rule. You can do that within service provider, e.g. in AppServiceProvider :
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Carbon\Carbon;
use Validator;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Validator::extend('age_match', function($attribute, $value, $parameters, $validator) {
$camper = \App\Models\Camper::find($parameters[0]);
$camp = \App\Models\Camp::find($parameters[1]);
$now = Carbon::now();
$age = $now->diffInYears($camper->birthdate);
return $age >= $camp->min_age && $age <= $camp->max_age;
});
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
}
}
Latter, validation is something like:
....
$a = $request->all();
$validator = Validator::make($a, [
'camp_id' => 'required|numeric',
'camper_id' => 'required|numeric|age_match:' .$a['camper_id'] . ',' . $a['camp_id'],
]);
...
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community