Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to Add Values to Request Array in Laravel

How to Add Values to Request Array in Laravel ?

Aman Jain, August 9, 2022March 16, 2024

In laravel to get the request data we use request class or function but sometimes we want to add values to request array in laravel, so to add extra values or we can say to merge to our own custom values to laravel request array we need to use merge method of request class.

In this article i will show you to add extra values to request using merge method and also we will cover to add array values in existing request.

We will use simple example and multiple methods to add values to existing request class. this example will work with any laravel 5, laravel 6, laravel 7, laravel 8 and laravel 9.

Suppose we have a request form and we want to add user_id key to $request variable in laravel so to achieve this we can simply use as follow

<?php

namespace App\Http\Controllers;

use App\Models\Article;
use Illuminate\Http\Request;

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
      $request->merge(["user_id"=>1]);
 
    }
}

so here we used $request->merge(["user_id"=>1]); to merge the user id field.

Add array of data to request in laravel

As we can see above we easily added the extra params to request and now in the same way we can add array in request as follow

$request->merge(["user_id"=>["a"=>"1","b"=>3]]);

so complete controller

<?php

namespace App\Http\Controllers;

use App\Models\Article;
use Illuminate\Http\Request;

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
      $request->merge(["user_id"=>["a"=>"1","b"=>3]]);
 
    }
}

Screenshot:

request merge laravel
request merge laravel

Updating array field of request in laravel

Let’s suppose we have a form which consist form array and we want to update or add some values to array field then we can even use same merge request but before merge we need to extract it and then we will modify the array. suppose we have multiple ids in array request as follow

[1,2,3] // form field user_ids

then if we want to add more data to it then we need to extract it from request array first

$userids = $request->user_ids;
$userids[]=4;
$userids[]=5;

now merge it with old request as follow

$request->merge(["user_ids"=>$userids]);

Also Read : How to Get Headers Data from Request in Laravel ?

Related

Php Laravel Laravel 9 add newlaravelmerge

Post navigation

Previous post
Next post

Related Posts

Php How to Get Headers Data from Request in Laravel

How to Get Headers Data from Request in Laravel ?

June 9, 2022November 10, 2023

In this article we will learn to get headers data from request in laravel. Http Request headers are used to send the information to server like what type of content clients accept, Content type or authorizations technique used to validate the request.To retrieve the headers data in laravel we need…

Read More
Php Laravel 8 generate qr code

How to generate qr code in Laravel 8?

October 16, 2021November 16, 2021

In this article we will learn to generate the qr code in Laravel. we will use a simple example to generate the qr code. As we know qr code is stand for Quick Response and we can scan and get the information from it. we can use qr code in…

Read More
Php Laravel where or condition with example

How to use laravel where or condition with example ?

January 15, 2022February 19, 2022

Laravel eloquent provides multiple ways to build the query one the of the feature of laravel eloquent is creating dynamic query based on condition or complicated queries.In this article i will show you to build where or condition in laravel with example. I will show you multiple example to create…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Transformative Power of Education in the Digital Age
  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version