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 Create Enum Field in Laravel Migration

How to Create Enum Field in Laravel Migration ?

June 30, 2022August 17, 2022

In laravel while creating table schema for some columns we want to add column type enum field so in this article we will create Enum Field in Laravel Migration. We can easily add enum column as we add other varchar integer etc columns in laravel. Enum is useful when we…

Read More
Php How to set timezone dynamically and globally in Laravel

How to set timezone dynamically and globally in Laravel ?

September 20, 2022March 16, 2024

Laravel stores timezone configurations in config/app.php file and we can easily set timezone dynamically and globally in Laravel. Laravel usage carbon library to get the current date time and format, carbon also respects the timezone defined in config/app.php file. After php 5, php also introduced DateTime class to conduct the…

Read More
Php How to change data type of column in laravel migration

How to change data type of column in laravel migration ?

February 20, 2022November 5, 2023

In this blog post we will learn to change data type of column in laravel migration. Laravel covers most of the migration features like add, delete, indexing etc. but to modify the table like renaming column, change data type column to existing table laravel uses a separate package doctrine/dbal. In…

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

  • 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

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version