In Laravel How to Add Values to a Request Array

In Laravel is there a way to add values to a request array?

Usually, you do not want to add anything to a Request object, it's better to use collection and put() helper:

function store(Request $request) 
{
// some additional logic or checking
User::create(array_merge($request->all(), ['index' => 'value']));
}

Or you could union arrays:

User::create($request->all() + ['index' => 'value']);

But, if you really want to add something to a Request object, do this:

$request->request->add(['variable' => 'value']); //add request

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

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());

Add nested array to Laravel request

A better and faster approach is:

$event = $request->request->get('my-event');
$event['event-approved'] = "0";
$request->request->add(['my-event'=>$event]);

NOTE that it will NOT override any existing fields of my-event array except only set the event-approved field.

Or if you want a one-liner try:

$request->request->add(['my-event'=>array_merge($request->request->get('my-event'),['event-approved'=>"0"])]);

There is no other short-hand method for this kind of manipulation

adding float as values to a request array in laravel

Try this code

 $data = $request->only(
'user_id',
'status_id',
'currency_id',
'company_id',
'purchase_no',
'notes',
'delivery_date',
'publish'
);
$data['grandtotal'] = (float) str_replace(',', '', $request->grandtotal);
$orders = Orders::create($data);

Create new array in Laravel blade to create array within request

Try

<input type="checkbox" name="agreements[{{$agreement->id}}]" id="{{ $agreement->id }}" value="{{ $agreement->id }}">

How to add item to an array inside a collection in Laravel

The easiest way it to get the associative array from the request and play with it.

$myRequest = $request->all();
$myRequest['address'] = ['state' => 'test'];

otherwise, you have to modify the request object you need to add this code:

$request->merge([
'address' => $myRequest
]);

Doc: https://laravel.com/api/5.6/Illuminate/Http/Request.html#method_merge



Related Topics



Leave a reply



Submit