Support the ongoing development of Laravel.io →
Requests Laravel

How do you pass an argument to a function in this route?

I created a helper function, which I'm trying to call in api.php. But I'm getting the error that too few argument have been passed to helperFunctionCall(). I was trying to get the id of the post into that function call:

Route::get('post/{post}/helperFunctionCall',['uses' =>helperFunctionCall()],   function() use ($post) {
Session::put('post',$post);
});

I'm not at all sure Session is needed at all, I just saw that on stack overflow. But I do want to pass the id of the post ({post}) into the helperFunctionCall().

The helper function has this structure and lives in a helper.php file:

function helperFunctionCall($id) {
echo($id);
}
Last updated 3 years ago.
0
moderator

You could do the following:

// If you are using route model binding
Route::get('post/{post}/helperFunctionCall', function ($post) {
    helperFunctionCall($post->id);
});

// If you are not using route model binding and `{post}` in the URL is an ID
Route::get('post/{post}/helperFunctionCall', function () {
    helperFunctionCall($post);
});
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Ashley ashley Joined 9 Oct 2019

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.