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

concentrico's avatar

Multiple Form Requests

I'm building an API and I don't know which approach to take regarding form request. I have a users table which has name, lastname, email and password. The table has a polymorphic relationship with a users_admins table and users_clients table.

When I create a client, I need to create the user as well but all of it is sent in one request. How can I use two form requests (one for the users and one for the clients)?

What's the best way to approach this without mixing everything in one usersclients form request?

0 likes
2 replies
topvillas's avatar

Get what you need for the client and the user from the request as two separate arrays.

$client = $request->only(['something', 'something_else']);

$user = $request->only(['this', 'that']);

Client::create($client);

User::create($user);
florianhusquinet's avatar

I understand your point of view, though mixing everything in one single request to handle it as you wish might be a bit simpler, especially when dealing with validation.

If you absolutely prefer to separate both logic then I would do a nested api call, something like this (using VueJS):

methods: {
    createUser()
    {
        axios.post('/api/users', your_user_data)
                    .then( response => {
                            this.createClient(response.data.user);
                    }, error => {
                            // Handle errors like validation
                    });
    },
    createClient(user)
    {
        axios.post('/api/users/' + user.id +  '/clients', your_client_data)
                    .then( response => {
                            // Show success
                    }, error => {
                            // Handle errors like validation
                    });
    }
}

Please note that this small example does not take into consideration your validation.

Please or to participate in this conversation.