How to add variables to request()->all()? (Laravel 6.0)

laravel request
laravel add parameter to request in middleware
laravel put request
laravel request->merge
laravel request get url parameter
laravel get request headers
laravel get url parameters
laravel create request

I have Laravel 6.0 with something like this using the request()->all(); helper to create a new row on the database...

$input = request()->all();
Service::create($input);

I want to add two variables to it manually so that we don't need the user to add them manually. I have tried variations on this...

$time = time();
$input = array_merge(request()->all(), ['serviceSite' => 'companyname', 'serviceOrderedTime' => $time]);
Service::create($input);

Is there a way to do this with the request() helper or do I have to use an alternative method?

Try this.

$time = time();
$input = $request->all();
$input['serviceSite'] = 'companyname';
$input['serviceOrderedTime'] = $time;
Service::create($input);

Make sure serviceSite and serviceOrderedTime fillable in your model.

IF you want to merge it with $request then you can do like this.

$request->merge(["key"=>"value"]);
As your Way.
  $time = time(); 
  $request->request->add(['serviceSite' => 'companyname','serviceOrderedTime'=>$time]);
  Service::create($request->all());

Middleware, Is it bad to add data to $request in Middleware? I didn't find $this->group = Forum::findApprovedGroupBySlug(request()->route('group_slug')); $this->forum  Add those variables in now. … For the second part of the challenge, … look at the Add Contact request … and think about what other values … you might like to use as variables. … Add those variables to your environment file … and then put those variables … in the body of the Add Contact request. …

There is a helper function in laravel collection called add you can use it like this

$request->add(['serviceSite' => 'companyname']);
$request->add(['serviceOrderedTime' => $time]);
Service::create($request->all());

or you can make it by the common way

$request = request()->all();
$request['serviceSite'] = 'companyName';
$request['serviceOrderedTime'] = $time;

PHP: $_REQUEST, There is no need to do global $variable; to access it within functions or methods. Note: When running on the command line , this will not include the argv and argc​  In Django I want to add a variable to the request. i.e, def update_name(request): names = Employee.objects.filter() if names.count() > 0: request.update({"names": name }) re Stack Overflow Products

This is how you can set a variable to your request:

$request->request->set('serviceSite', 'serviceSite');
$request->request->set('serviceOrderedTime', $time);

In order to allow mass assignment, the serviceSite and serviceOrderedTime may be include in your $fillable model property.

Hope it helps.

Using Variables in Requests, You can insert variables in various parts of requests: Request URLs. Request header fields. URL parameters. HTML form fields in POST requests. Parameter  How to “artificially” add values to Request array May 3, 2016 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.

HTTP Requests - Laravel, Before using Redis sessions with Laravel, you will need to either install the with session data in Laravel: the global session helper and via a Request instance. Out of the box, whenever any catalog item is submitted, all the variables will be presented/shown on request item. It will be very difficult for the fulfiller who are working on the respective task…

HTTP Session - Laravel, Here is my pre-request script var ApplicationCode = request.data["​ApplicationCode"]; var Version=request.data["version"]; var  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'.

Raw json body how to add variable? - Help, You can include a _METHOD parameter in a POST request's body. The HTTP request must use the application/x-www-form-urlencoded content type. Mapping service request fields; Concatenating multiple inputs; You can add and manage questions in an SRD. You can also add conditions to display additional questions based on a user's response. See Adding branch conditions to questions. In addition, you can map variables to data sources, such as questions, hardcoded text strings, and fields.

Comments
  • You could make those columns have default values in DB already, not needing you to add them manually here?
  • Thx,, include in model is very important