Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mrperfectionist's avatar

how to pass data to controller in laravel from Route: Laravel 8

I have a Controller named: DateController which as a Method holiday()

I want to pass an Object to this holiday() method:

Route::get('/',
    [
        data: {
           'name' => 'Mr. Perfectionist'
        },
        DateController::class, 'holiday'
    ]
);

I just want to print it like this:

public function holiday($name) {
        return $name;
}

Giving Error:

ReflectionException Function () does not exist

0 likes
66 replies
MichalOravec's avatar

You can't, just save data to variable;

Route::get('/', [DateController::class, 'holiday']);
public function holiday()
{
    $name = 'Mr. Perfecionist';
}
2 likes
Snapey's avatar

You can use Get or post. Just get the value from the request object

eg $request->name

jlrdw's avatar

Could you read the chapter in the documentation on request. There are examples. And also the chapter covering routing.

mrperfectionist's avatar

@michaloravec,

Okay, Let me describe in details.

While I will browse / route then a Controller Function suppose holiday() will call. I don't want to pass anything from View. But I need to pass a FIXED name variable to my holiday() function.

After some calculation this function will give me a data. That's not a problem.

Got the point?

jlrdw's avatar

Then make a route with that fixed name.

MichalOravec's avatar

With this you have there a fixed name.

public function holiday()
{
    $name = 'Mr. Perfecionist';
}

It doesn't matter if it's GET or POST request.

jlrdw's avatar

Do you not understand the difference between request and a parameter being passed in the route.

Site/John

There John is a parameter not request.

Routing parameters are discussed and demonstrated in the routing chapter.

What are you after a parameter or a request object?

3 likes
mrperfectionist's avatar

@jlrdw,

Okay.

With Ajax call we can pass data to Controller. But I don't want to take any help of View. Want to pass a variable from Route to Controller.

Thanks

MichalOravec's avatar

Sorry, but I still don't get it. What does it mean from route to controller?

mrperfectionist's avatar

@michaloravec,

Please see this example:

Route::view('/',
    'welcome',
    ['name' => 'Taylor']
);

I can return a view directly with a name variable. Here, without return a view, I want to get just to call a controller function with the name variable.

Now clear to you? For Example:

Route::get('/',
    [UserController::class, 'index'],
    ['name' => 'Taylor']
);
mrperfectionist's avatar

@michaloravec,

I have tried with this:

public function holiday(Request $request) {
        return $request->all();
}

Route:

Route::get('/',
    [DateController::class, 'holiday'],
    ['name' => 'Taylor']
);

It is not giving any Error. But giving me an empty Array!!

jlrdw's avatar

And your controller method inside the parentheses get the parameter.

Public function someName ($name = null)
{
   echo $name;
automica's avatar

@mrperfectionist in this case, you want your User index method to return data rather than a view?

so:

// DateController

public function holiday(Request $request) {

$name = $request->name;

$data = $this->doSomethingWithName($name);

return response()->json($data);
}
MichalOravec's avatar

Where did you something like that?

This

Route::view('/welcome', 'welcome', ['name' => 'Taylor']);

is same as

Route::view('/welcome', [WelcomeController::class, 'index']);

with WelcomeController

public function index()
{
    return view('welcome', ['name' => 'Taylor]);
}

So it means you CANNOT do this

Route::get('/',
    [DateController::class, 'holiday'],
    ['name' => 'Taylor']
);
automica's avatar

@mrperfectionist I think it would be better to define the function of the code you are trying to write, rather than the detail.

can you describe what you are building. is it in JavaScript. how are you creating the data. what do you want to do with it when your holiday function runs?

1 like
mrperfectionist's avatar

Okay.

Again describing!!

While I will go to this URL ('/') my laravel Application it will show me a Name.

For this purpose, I don't want to load Any view.

Just with my root URL i want pass a Name to a Controller.

While we run a Fresh Laravel Solution, it show me welcome.blade.php file. Because in our Route we declare it to load, Right??

I want to load just a Controller Method with a Name Variable which I want to pass from Route.

Clear??

Snapey's avatar

You cannot pass data into the controller from the routes file.

Create a different route and controller method for each case

Route::get('/bill','holidayBill');
Route::get('/john','holidayJohn');
Route::get('/fred','holidayFred');
Route::get('/paul','holidayPaul');

controller

public function holidayBill()
{
	return holiday('MrSmith');
}

public function holidayJohn()
{
	return holiday('MrJones');
}

public function holidayFred()
{
	return holiday('MrPurple');
}

public function holidayPaul()
{
	return holiday('MrBlue');
}

public function holiday($person)
{

}
mrperfectionist's avatar

Okay.

Then forgot about Route.

Is it possible to Call a Controller from index.php file??

Next

Please or to participate in this conversation.