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

1public function store(Request $request)
2{
3 $validationArray = [
4 'title' => 'required',
5 'company' => 'required',
6 'logo' => 'file|max:2048',
7 'location' => 'required',
8 'apply_link' => 'required|url',
9 'content' => 'required',
10 'payment_method_id' => 'required'
11 ];
12 
13 if (!Auth::check()) {
14 $validationArray = array_merge($validationArray, [
15 'email' => 'required|email|unique:users',
16 'password' => 'required|confirmed|min:5',
17 'name' => 'required'
18 ]);
19 }
20 //
21}

Like our articles?

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