Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 577
sdexp Avatar asked Nov 22 '25 03:11

sdexp


2 Answers

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());
like image 123
Dilip Hirapara Avatar answered Nov 24 '25 19:11

Dilip Hirapara


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;
like image 42
Joseph Avatar answered Nov 24 '25 20:11

Joseph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!