Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add / remove elements from array that is in Request

My request looks like this

Array
(
  [name] => Eugene A
  [address] => Array
    (
        [billing] => Array
            (
                [address] => aaa
            )
        [shipping] => Array
            (
                [address] => bbb
            )
    )
)

I need to delete the shipping address. But how?

I can only delete both addresses,

$request->request->remove('address');

but I don't want it.

I want to delete only shipping address, like so

$request->request->remove('address.shipping');

But it is not working for me

Laravel 5.6

Update

Why do I need it?

Easy. I have abstracted out my Form Request validation into a class that is a child to Illuminate\Foundation\Http\FormRequest. I actually have few classes for validation. I call them one by one in a controller like so:

app()->make(CustomerPostRequest::class); // validate Customer information
app()->make(AddressSaveRequest::class); // validate Addresses

Why?

Now I can Mock this requests in unit-tests, and I can have my validation abstracted out. And I can use Address validation in many places.

But Now I need more flexibility. Why?

Because AddressSaveRequest rule looks like this

public function rules(): array
  {
    return [
        'address.*.address'    => [
            'bail',
            'required',
            'string',
        ],
   ...

It validates all addresses. But sometimes I don't want to validate shipping address, if the the chech_box - ship_to_the_same_address is ticked.

But I have my Address validator abstracted in separate file and it is used in many places. There are places where ship_to_the_same_address tick box is not presented.

Thus I cannot use 'required_unless:ship_to_same_address,yes',

And I cannot use

app()->makeWith(AddressSaveRequest::class, ['ship_to_the_same_address ' => 'yes']);

Because Taylor said ...when calling makeWith. In my opinion it should make a new instance each time this method is called because the given parameter array is dynamic.. And it does, and it does not work correctly with app()->instance(AddressSaveRequest::class, $addressSaveRequest); and cannot be mocked in unit tests.

Why Taylor decided it - I seriously don't know.

PS And yes, I know that mocking requests is not recommended.

like image 731
Yevgeniy Afanasyev Avatar asked Oct 16 '25 07:10

Yevgeniy Afanasyev


1 Answers

If you were trying to add or remove inputs from the Request itself:

You can add data to the request pretty easily by merging it in and letting Laravel handle which data source is being used:

$request->merge(['input' => 'value']);

That will merge in the input named input into the input source for the Request.

For removing inputs you could try to replace all the inputs without that particular input in the replacement:

$request->replace($request->except('address.shipping'));

Just one idea to try.

like image 180
lagbox Avatar answered Oct 17 '25 19:10

lagbox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!