Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

LorienDarenya's avatar

$this->request->get() doesn't work inside FormRequest

The following form request is what I am attempting to create. It throws an invalid argument supplied for foreach error.

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class SaveCatRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
          'age' => 'required|string|in:cub,adolescent,adult',
          'gender' => 'required|string|in:male,female',
          'species' => 'required|numeric',
          'pelt' => 'required|numeric',
          'nose' => 'required|numeric',
          'mane' => 'required|numeric',
          'pawpad' => 'required|numeric',
          'eye' => 'required|numeric'
      ];
    $markings = [];
        foreach ($this->request->get('m') as $m) {
            $mark = explode(",", $m);
            array_push($markings, (object) ['name' => $mark[0], 'color' => $mark[1], 'opacity' => floatval($mark[2])/100.0]);
            foreach ($markings as $key => $val) {
                $rules .= [
                    'name.*' => 'required|exists:markings',
                    'color.*' => 'required|exists:marking_colors,name',
                    'opacity' => 'required|between:0,100'
                ];
            }
        }
        return $rules;
    }
}

What am I doing wrong here?

0 likes
6 replies
azimidev's avatar

why don't you use request() helper? did you do dd($this->request) to see if it returns anything at all?

LorienDarenya's avatar

I ended up making a rule for it. Though right now I'm not sure where to place the rule. It's currently in the formRequest.

Snapey's avatar
Snapey
Best Answer
Level 122

the form request is an extension of the request class

All you need is $this

1 like
Cronix's avatar

Additionally, this doesn't make sense:

foreach ($markings as $key => $val) {
                $rules .= [
                    'name.*' => 'required|exists:markings',
                    'color.*' => 'required|exists:marking_colors,name',
                    'opacity' => 'required|between:0,100'
                ];
            }

You're just adding the exact same rules over and over. You just need it once, so don't need a loop?

$rules is also an array, but you're using string concatenation on it with .=, which wouldn't work and should be producing an error...

$rules .= [
nolros's avatar

@LorienDarenya as @Snapey said your extending FormRequest which extends Request so all you need is $this->get() as $this === request() in this class

Please or to participate in this conversation.