Home > Enterprise >  How to get particular value from the $request object in Laravel?
How to get particular value from the $request object in Laravel?

Time:12-14

I have an API which is responsible for downloading the excel sheet , my request object contains some values based on the request object, I want to get the user_ID value please help me to achieve this.

dd($request)

^ App\Containers\Charge\UI\API\Requests\GetAllUsersRequest {#1638
  #transporter: "App\Containers\Charge\Data\Transporters\GetAlluserssTransporter"
  #access: array:2 [
    "permissions" => "list-payments"
    "roles" => ""
  ]
  #decode: array:2 [
    "id" => "user_id"
    "customer_id" => "cus"
  ]
  #urlParameters: array:1 [
    0 => "id"
  ]
  #map: []
  #account_id: 1313
  #merchant_id: 9809
  #user_id: 143

Controller.php

public function download(GetAllUsersRequest $request){
$request->user_id; //it's taking null
}

CodePudding user response:

For a direct variable you can use:

$user_ID = $request->input('user_ID');

Alternatively:

$req = $request->all();
$user_ID = $req['user_ID'];

# Debugging:
dd($request->all());

CodePudding user response:

If your request has for instance an ID, name and email you can access each attribute by simply doing this:

$request->email

$request->ID 

$request->name

It works as Grant stated, but with this you save some brackets

  • Related