Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a Laravel Request object on the fly

I'm handling data in one controller and want to pass it further into another controller to avoid duplicate code.

Is there a way to set up a Request object that is needed in the other controller's store-method? I've traced down the Request inheritance and came to Symfony's Request object which has a request property that is in fact a ParameterBag that holds a method add to add parameters with values to it.

I've tried the following but I'm getting null as result:

$myRequest = new Request(); $myRequest->request->add(['foo' => 'bar']); var_dump($myRequest->foo); 

I'm on Laravel 5.1 for this project.

like image 680
Ben Fransen Avatar asked Oct 26 '16 12:10

Ben Fransen


People also ask

How do I create a new request in laravel?

You can use replace() : $request = new \Illuminate\Http\Request(); $request->replace(['foo' => 'bar']); dd($request->foo); Alternatively, it would make more sense to create a Job for whatever is going on in your second controller and remove the ShouldQueue interface to make it run synchronously. Hope this helps!

What is request -> input () in laravel?

input() is a method of the Laravel Request class that is extending Symfony Request class, and it supports dot notation to access nested data (like $name = $request->input('products.0.name') ).

How do I request a file in laravel?

The hashName method is exactly what Laravel calls in the store method. $request->image->hashName(); You will get the same name that Laravel generates when it creates the file name during the store method. $path = $request->image->getClientOriginalName();


1 Answers

You can use replace():

$request = new \Illuminate\Http\Request();  $request->replace(['foo' => 'bar']);  dd($request->foo); 

Alternatively, it would make more sense to create a Job for whatever is going on in your second controller and remove the ShouldQueue interface to make it run synchronously.

Hope this helps!

like image 171
Rwd Avatar answered Sep 18 '22 21:09

Rwd