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);
}
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);
});
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community