How to add custom variable to URL Request of controller in laravel?

Issue

I have added this to the Controller:

public function updateMethod(Request $request)
{
   $i = 1;
   dd($request->input_name_1);
}

Now I wanted to change to this:

public function updateMethod(Request $request)
    {
       $i = 1;
       dd($request->input_name_$i);
    }

But I get this error:

syntax error, unexpected ‘$i’ (T_VARIABLE), expecting ‘)’

So how can I add my custom variable to $request properly?

Solution

You can add custom variables by using bind string in one variable and passing it to $request->
like below code

$i = 1;
$tmp  = "input_name_".$i;
dd($request->$tmp);

using this you can get values of custom variables

Answered By – Siddharth

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published