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

signalwarrant's avatar

Passing form values to external API

I need to pass a value ($cust_id) that a user enters into a form to an http request of an external API.

This is my controller. Because I have no $cust_id value set the API defualts to my customer ID.

public function member_Yearly()
    {
        //@param integer ['cust_id'] Defaults to the authenticated member.
        //@return mixed
        $yearly = $this->service->stats->member_yearly();
        dd($yearly);
    }

This is my very basic form

<form action="#" method="POST">
            @csrf
            <div class="mb-6 max-w-md">
                <label for="search" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Your
                    Customer ID</label>
                <input type="custid" id="custid"
                    class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" required>
            </div>
            <button type="submit"
                class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm w-full sm:w-auto px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">Submit</button>
        </form>

Can someone explain the best or preferred method to do this? Thanks in advance.

0 likes
3 replies
webrobert's avatar

its just like any other input from a user. Validate it. Then send it thru, but you haven't shown any of the logic for the api. I assume you have that part figured out?

Yorki's avatar
Yorki
Best Answer
Level 11

Just use the custid from request and pass into your api client, something like:

public function member_Yearly(Request $request)
{
   $yearly = $this->service->stats->member_yearly($request->input('custid'));
   dd($yearly);
}

And change your input type to text add name on your input so its passed on request to your controller:

<input type="text" id="custid" name="custid" ... />
1 like
signalwarrant's avatar

Thanks @yorki for your assistance. For anyone else trying to wrap their head around this scenario this is what I landed on that works.

I created a request class like this

php artisan make: request UserRequest

This is my Request class code. In my case I only need to validate the cust_id passed in from the form. Make sure you return true in the authorize function.

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UserRequest 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<string, mixed>
     */
    public function rules()
    {
        return [
            'cust_id' => 'required|int'
        ];
    }
}

My controller is essentially unchanged.

public function member_Yearly(UserRequest $request)
    {
        //@param integer ['cust_id'] Defaults to the authenticated member.
        //@return mixed
        $validated = $request->validated();
        $yearly = $this->service->stats->member_yearly($validated);
    }

Made the changes to my frontend form that were mentioned above.

<form action="yearly" method="POST">
            @csrf
            <div class="mb-6 max-w-md">
                <label for="search" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Your
                    Customer ID</label>
                <input type="text" id="cust_id" name="cust_id"
                    class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" required>
            </div>
            <button type="submit"
                class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm w-full sm:w-auto px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">Submit</button>
        </form>

From reading other docs it seems the request class is the standard way to handle form requests. Once I'm feature complete I will have a few forms on the site. Is it best practice to have a separate request class for each form?

Please or to participate in this conversation.