c319113
c319113

Reputation: 215

Laravel get the parameter of url in controller

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

Upvotes: 3

Views: 5546

Answers (4)

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

Upvotes: 0

Yoonas T K
Yoonas T K

Reputation: 21

Please use the id in url as controller function parameters

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

Upvotes: 2

Mayur Panchal
Mayur Panchal

Reputation: 655

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);
}

Upvotes: 7

dynero
dynero

Reputation: 300

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'));
}

Upvotes: 0

Related Questions