How to "artificially" add values to Request array

A few times I encountered a situation - a store() or update() method with Request parameter, but I needed to add some additional value to the request before calling Eloquent functions. So how to do that? Apparently, pretty easy. Let's look at the code:
function store(Request $request)
{
  // some additional logic or checking
  User::create($request->all());
}
What if you need to add additional field to the request with value coming from that additional logic block? It would look like this:
function store(Request $request)
{
  // some additional logic or checking
  $plan = 123; // some logic to decide user's plan
  $request->request->add(['plan' => $plan]);
  User::create($request->all());
}
See? The general logic is to add an array of key => value to the $request->request property. Small tip, but hopefully helpful!

No comments yet…

Like our articles?

Become a Premium Member for $129/year or $29/month

Written by