Rules depending on some other conditions

If your rules are dynamic and depend on some other condition, you can create that array of rules on the fly

public function store(Request $request)
{
$validationArray = [
'title' => 'required',
'company' => 'required',
'logo' => 'file|max:2048',
'location' => 'required',
'apply_link' => 'required|url',
'content' => 'required',
'payment_method_id' => 'required'
];
 
if (!Auth::check()) {
$validationArray = array_merge($validationArray, [
'email' => 'required|email|unique:users',
'password' => 'required|confirmed|min:5',
'name' => 'required'
]);
}
//
}

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 68 courses (1183 lessons, total 43 h 18 min)
  • 90 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent New Courses