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());
}
$request->offsetSet(key, value);
I used this code to add something to my request.
$req->query->add(['key'=>'variable']);
$req->request->add(['key'=>'variable']);
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());
}
you can use laravel helper and request() magic method ...
request()->request->add(['variable1'=>'value1','variable2'=>'value2']);
I tried $request->merge($array)
function in Laravel 5.2 and it is working perfectly.
Example:
$request->merge(["key"=>"value"]);
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);
}
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.
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
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
You can access directly the request array with $request['key'] = 'value'
;
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' ]);
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]);
}
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
You can also use below code
$request->request->set(key, value).
Fits better for me.
Source: Stackoverflow.com