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

alihoushyaripour's avatar

How to call a controller function that has the request class input?

Hi,

I have a controller with two function that I need to call upload function from add function while upload function has the UploadRequest class as input.

How can I make an instance of UploadRequest class, and call upload function?!

Controller class:

class ImageController extends Controller
{
    public function add(Request $request)
    {
        // code

        // call $this->upload()
    }

    public function upload(UploadRequest $request)
    {
        // validate
        $request = $request->validated();

        // code
    }
}

UploadRequest class:

class UploadRequest 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()
    {
        return [
            'type' => 'required|string|in:avatar,place,review,meta',
            'id' => 'required|numeric|integer|regex:/^[0-9]+$/'
        ];
    }
}
0 likes
2 replies
NOMGUY's avatar

i believe you should create a Respository instead of a new class. It minimizes your efforts.

alihoushyaripour's avatar

@NOMGUY

Ok, what repository should I write?

I can't pass type and id parameters as key-value to UploadRequest class manual, my problem is this.

Please or to participate in this conversation.