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

uglik's avatar
Level 3

Merge form request

Hello, everyone

I can't figure out how to combine the request form. I have a field active, a checkbox toggle switch. When this is checked, I have active in the request, otherwise it is not.

array:3 [▼
  "name" => "Georgia"
  "currency_id" => "2"
  "active" => "1"
]
 array:2 [▼
  "name" => "Georgia"
  "currency_id" => "2"
]

Active field boolean. Update method in controller. I'm trying to check if a field is actively present set the value to true otherwise not false.

    public function update(Country $country, UpdateCountryRequest $request)
    {

        $is_active = $request->has('active'); // true | false
        $request->merge(['active' => $is_active]);
        
        $country->update($request->validated());

        return redirect()->route('country.index')->with('status', 'Country updated!');
    }

My request form class

    public function rules()
    {

       // $this->merge(['active' => $this->has('active')]);

        return [
            'name' => 'required|string|max:255|unique:countries,name,'. $this->country->id,
            'currency_id' => 'required|numeric|exists:currencies,id',
            'active' => 'boolean'
        ];
    }

But when i call $request->validated(), the state is not checked, active is still missing in the request. Can you please tell me how to add an active field to the request and check the validation?

0 likes
3 replies
Sinnbeck's avatar

There is a helper for booleans

$is_active = $request->boolean('active');
uglik's avatar
Level 3

@Sinnbeck I can't add actively so that everything is present in the request. For example, if actively present, assign true, and if absent, add actively and assign false

Snapey's avatar

You can do this instead

$country->update($request->validated() + ['active' => $request->has('active')]);

And don't bother validating it, just treat it as present = true, missing = false.

1 like

Please or to participate in this conversation.