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 Laravel 11 CRUD Example Tutorial with Search, Image and Pagination

Laravel 11 CRUD Example Tutorial with Search, Image and Pagination

July 5, 2024July 5, 2024

In this article, we’ll delve into Laravel and explore the implementation of CRUD operations alongside essential features like Search, Image uploading, and Pagination. CRUD, an acronym for Create, Read, Update, and Delete, forms the backbone of database management in web applications across all programming paradigms. This tutorial covers these fundamental…

Read More
Php How to get random rows in laravel example

How to get random rows in laravel example ?

October 10, 2022March 16, 2024

In this blog post, we’ll take a look at how to get random rows in Laravel database. We’ll explore the use of the QueryBuilder, Eloquent, and the DB facade. laravel has inbuilt eloquent method to get the random records from database using the inRandomOrder method. we will understand the random…

Read More

Laravel 10 – Error During installation laravel/framework[v10.0.0] require composer-runtime-api ^2.2 – Solved

March 8, 2023March 16, 2024

Today i was installing laravel 10 with php version 8.2 and i encountered an issue laravel/framework[v10.0.0, …, v10.3.2] require composer-runtime-api ^2.2 -> found composer-runtime-api[2.1.0] but it does not match the constraint. This issue occurred due to old composer. So we can quickly fix this by running below command May be…

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

  • 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 Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version