Laravel is there a way to add values to a request array
PhpLaravelPhp Problem Overview
I come across a situation in Laravel while calling a store() or update() method with Request parameter to add some additional value to the request before calling Eloquent functions is there any way for that.
function store(Request $request)
{
// some additional logic or checking
User::create($request->all());
}
Php Solutions
Solution 1 - Php
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 - Php
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 - Php
I tried $request->merge($array)
function in Laravel 5.2 and it is working perfectly.
Example:
$request->merge(["key"=>"value"]);
Solution 4 - Php
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 - Php
You can also use below code
$request->request->set(key, value).
Fits better for me.
Solution 6 - Php
In laravel 5.6 we can pass parameters between Middlewares for example:
FirstMiddleware
public function handle($request, Closure $next, ...$params)
{
//some code
return $next($request->merge(['key' => 'value']));
}
SecondMiddleware
public function handle($request, Closure $next, ...$params)
{
//some code
dd($request->all());
}
Solution 7 - Php
simply use this:-
$request->merge(['index' => 'value']);
work for me
This will add a new value to your request object with the given index and use can use it in your function or while saving all request data into the database.
Solution 8 - Php
Based on my observations:
$request->request->add(['variable' => 'value']);
will (mostly) work in POST, PUT & DELETE methods, because there is value(s) passed, one of those is _token
. Like example below.
<form action="{{ route('process', $id) }}" method="POST">
@csrf
</form>
public function process(Request $request, $id){
$request->request->add(['id' => $id]);
}
But [below code] won't work because there is no value(s) passed, it doesn't really add.
<a href='{{ route('process', $id) }}'>PROCESS</a>
public function process(Request $request, $id){
$request->request->add(['id' => $id]);
}
When using GET method you can either declare Request and assign value(s) on it directly. Like below:
public function process($id){
$request = new Request(['id' => $id]);
}
Or you can use merge
. This is better actually than $request->request->add(['variable' => 'value']);
because can initialize, and add request values that will work for all methods (GET, POST, PUT, DELETE)
public function process(Request $request, $id){
$request->merge(['id' => $id]);
}
Tag: laravel5.8.11
Solution 9 - Php
To add a new parameter for ex: newParam
to the current Request
Object, you can do:
$newParam = "paramvalue";
$request->request->add(['newParam' => $newParam]);
After adding the new parameter, you would be able to see this newly added parameter to the Request object by:
dd($request);//prints the contents of the Request object
Solution 10 - Php
I used this code to add something to my request.
$req->query->add(['key'=>'variable']);
$req->request->add(['key'=>'variable']);
Solution 11 - Php
you can use laravel helper and request() magic method ...
request()->request->add(['variable1'=>'value1','variable2'=>'value2']);
Solution 12 - Php
You can access directly the request array with $request['key'] = 'value'
;
Solution 13 - Php
The best one I have used and researched on it is $request->merge([])
(Check My Piece of Code):
public function index(Request $request) {
not_permissions_redirect(have_premission(2));
$filters = (!empty($request->all())) ? true : false;
$request->merge(['type' => 'admin']);
$users = $this->service->getAllUsers($request->all());
$roles = $this->roles->getAllAdminRoles();
return view('users.list', compact(['users', 'roles', 'filters']));
}
Check line # 3 inside the index function.
Solution 14 - Php
You can add parameters to the request from a middleware by doing:
public function handle($request, Closure $next)
{
$request->route()->setParameter('foo', 'bar');
return $next($request);
}
Solution 15 - Php
If you would like to add a query param to the Request object that can be obtained by calling
$request->get('key')
OR
$request->all()
The following works well for me.
$request->query->set('key','value');
Solution 16 - Php
You can also do $request['key'] = 'value';
on newer Laravel versions.