3

I'm trying to get a value of the parameter in to a variable this is what I have so far:

public function getname(Request $request)
{
    echo ($request->input('id'));

    return view('test.test1');
}

and my route is:

Route::get('/test/{id}','Controller@getname');

the output I get is NULL how can I get the value of the url parameter?

my url is:

localhost/test/test1/4

so I want 4 to be outputed.

I tried doing thr request method but didn't work so it's not the same as Passing page URL parameter to controller in Laravel 5.2

3

4 Answers 4

7

web/routes.php

Route::get('/test/{id}','Controller@getname');

Controller file

public function getname(Request $request,$id)
{

    echo $id; # will output 4
    $param = $id;
    return view('test.test1')->with('param',$param);
}
Sign up to request clarification or add additional context in comments.

Comments

2

Please use the id in url as controller function parameters

public function something(Request $request,$id){
    return $id;
} 

1 Comment

The first comma in the parameter list is not supposed to be there public function something(Request $request, $id){ return $id; }
0

You should add extra parameters to your getName function.

# Stick to the convention of camelcase functions, not getname but getName
public function getName(Request $request, $param, $id)
{
    # This only works when you pass an id field while performing a POST request.
    # echo ($request->input('id'));
    echo $param; # will output 'test1'
    echo $id; # will output 4
    return view('test.test1', compact('$param','id'));
}

Comments

0

very simple, you can follow this code in your controller

$url = url()->current();
$url = str_replace("http://", "", $url);
dd($url);

output: localhost/test/test1/4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.