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

Giolf's avatar

bind FormRequest implementations

I've some FormRequest very similar.
For example in my project you can edit personal info of student, teacher and contacts. The personal info of each entity are the same, the only thing that change is that a teacher has more personal info than a student but the fields are the same.

The thing that i would like to do is Extends this similar FormRequests with an abstract FormRequest parent class.
In this way in my controller i could give the only one dependency ( FormRequest parent class) and laravel for me inject the right child FormRequest class.

Probably i've to work with a service provider but i don't know in which way ... i read the documentation but i didn't find something similar to this case.

0 likes
5 replies
Giolf's avatar

Because each profile's entity has different section to edit, The personal info section the accounting section, ect ect ... every section can be edited individually. What i would like to do (for each entity) is to have just one edit method. For example for the StudentController could be:

editStudent(ParentFormRequest $request)

Where inside it will be inject the right FormRequest.
The alternative i think is make more methods like:

public function editPersonalInfoStudent(EditPersonalInfoRequest $request)
{
    // edit personal info's student
}
public function editScholarInfoStudent(EditScholarInfoRequest $request)
{
    // edit scholar info's student
}

but i think is much better have just one method that use the right FormRequest ... no ?

Giolf's avatar
Giolf
OP
Best Answer
Level 2

I discovered that FormRequest can perform a really strong and powerfull Logic so i don't need this type of way to reach my target but i can just performs this logic inside the request to get the sets of rules that i need.

JarekTkaczyk's avatar

@Giolf Yeah, you can do whatever needs to be done in the form request. That is why you have rules, messages and other stuff as functions there, not properties. This way you are free to adjust it for every single request class.

My question was not about the reason for polymorphism, but trying to use IoC binding for this.

Giolf's avatar

@JarekTkaczyk The reason was that i saw this topic:
https://laracasts.com/discuss/channels/general-discussion/multiple-services-implementing-same-interface-switching-at-runtime#

In this topic you explain how to bind at runtime ... so i was thinking:
"Why don't bind a FormRequest at runtime too ???"
In this way inside the edit method of my controllers i can use the same ParentClass variable without to be worries about which FormRequest i'm using because the IoC did the right bind for me.

Because in my app u can edit a teacher a student and a Contact, but each one of them has many different thing splitted by section to edit. so my idea was to don't use every single method to edit every single section of them. but just use a single edit method that use a ParentFormRequest but how can i get the right FormRequest?.
With google i founded the method that u explained with runtime binding. But then i discovered a simple way to return the set of rules that i want (with a little bit of logic) inside the rules function ... so i can still have just one edit method that get one general form request where inside of this one i can manage the logic ... where in this case the logic is to return the right rules for editing the section that the user want to update ... just som if else statment and is fine. You are right, if the FormRequest can performs logic ... why don't use it ???

Please or to participate in this conversation.