How to change value of a request parameter in laravel

Laravel

Laravel Problem Overview


I need to change value of my request parameter like this:

$request->name = "My Value!";

I use this code but does not work:

$request->offsetSet('img', $img);

Laravel Solutions


Solution 1 - Laravel

Try to:

$requestData = $request->all();
$requestData['img'] = $img;

Another way to do it:

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

Thanks to @JoelHinz for this.

If you want to add or overwrite nested data:

$data['some']['thing'] = 'value';
$request->merge($data);

If you do not inject Request $request object, you can use the global request() helper or \Request:: facade instead of $request

Solution 2 - Laravel

Use merge():

$request->merge([
    'user_id' => $modified_user_id_here,
]);

Simple! No need to transfer the entire $request->all() to another variable.

Read more about Laravel's merge() here:

https://laravel.com/docs/collections#method-merge

Solution 3 - Laravel

If you need to customize the request

$data = $request->all();

you can pass the name of the field and the value

$data['product_ref_code'] = 1650;

and finally pass the new request

$last = Product::create($data);

Solution 4 - Laravel

If you need to update a property in the request, I recommend you to use the replace method from Request class used by Laravel

$request->replace(['property to update' => $newValue]);

Solution 5 - Laravel

Use add

$request->request->add(['img' => $img]);

Solution 6 - Laravel

If you use custom requests for validation, for replace data for validation, or to set default data (for checkboxes or other) use override method prepareForValidation().

namespace App\Http\Requests\Admin\Category;
    
class CategoryRequest extends AbstractRequest
{
    protected function prepareForValidation()
    {
        if ( ! $this->get('url')) {
            $this->merge([
                'url' => $this->get('name'),
            ]);
        }
        $this->merge([
            'url'    => \Str::slug($this->get('url')),
            'active' => (int)$this->get('active'),
        ]);
    }
}

I hope this information will be useful to somebody.

Solution 7 - Laravel

It work for me

$request = new Request();
$request->headers->set('content-type', 'application/json');     
$request->initialize(['yourParam' => 2]);

check output

$queryParams = $request->query();
dd($queryParams['yourParam']); // 2

Solution 8 - Laravel

Great answers here but I needed to replace a value in a JSON request. After a little digging into the code, I came up with the following code. Let me know if I'm doing something dumb.

$json = $request->json()->all();
$json['field'] = 'new value';
$request->json()->replace($json);

Solution 9 - Laravel

Try that :

$request["name"] = "My New Value";
$request["img"]  = $img;

It's worked in Laravel 8.

Solution 10 - Laravel

Also, make sure to update the model class.

Item 
{
    fillable=[
        'img',
        ... // other attributes
    ];
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMorteza NegahiView Question on Stackoverflow
Solution 1 - LaravelAlexey MezeninView Answer on Stackoverflow
Solution 2 - LaraveldoncadavonaView Answer on Stackoverflow
Solution 3 - LaravelAlaa MoneamView Answer on Stackoverflow
Solution 4 - LaravelJa OuadView Answer on Stackoverflow
Solution 5 - LaravelLuís FernandoView Answer on Stackoverflow
Solution 6 - LaravelOlegView Answer on Stackoverflow
Solution 7 - LaravelPhucView Answer on Stackoverflow
Solution 8 - LaravelphoenixView Answer on Stackoverflow
Solution 9 - LaravelWaseem AlhabashView Answer on Stackoverflow
Solution 10 - LaravelIsuru DilshanView Answer on Stackoverflow