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 or questions yet...

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 59 courses (1056 lessons, total 42 h 44 min)
  • 78 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials