Laravel is there a way to add values to a request array
Solution 1:
Usually, you do not want to add anything to a Request object, it's better to use collection and put()
helper:
function store(Request $request)
{
// some additional logic or checking
User::create(array_merge($request->all(), ['index' => 'value']));
}
Or you could union arrays:
User::create($request->all() + ['index' => 'value']);
But, if you really want to add something to a Request object, do this:
$request->request->add(['variable' => 'value']); //add request
Solution 2:
Referring to Alexey Mezenin
answer:
While using his answer, I had to add something directly to the Request Object and used:
$request->request->add(['variable', 'value']);
Using this it adds two variables :
$request[0] = 'variable', $request[1] = 'value'
If you are a newbie like me and you needed an associate array the correct way to do is
$request->request->add(['variable' => 'value']);
Hope I saved your some time
PS: Thank you @Alexey
, you really helped me out with your answer
Solution 3:
I tried $request->merge($array)
function in Laravel 5.2 and it is working perfectly.
Example:
$request->merge(["key"=>"value"]);
Solution 4:
enough said on this subject but i couldn't resist to add my own answer. I believe the simplest approach is
request()->merge([ 'foo' => 'bar' ]);
Solution 5:
You can also use below code
$request->request->set(key, value).
Fits better for me.