Craig Johnstone
Craig Johnstone

Reputation: 131

url parameter - laravel

our users access our site with a unique parameter on the url. ie http://example.com/hire-agreement?u=unique_param

I've set up a route to a view -

Route::get('/hire-agreement', function () {
    return view('hire-agreement');
});

I have 2 questions.

  1. Do I need to add anything else to the Route to allow the parameter to be read in the view?
  2. How do I read this parameter value in the View? Can I use $_GET["name"]) ?

thanks Craig.

Upvotes: 1

Views: 130

Answers (3)

AxissXs
AxissXs

Reputation: 731

You better pass the request to a controller and handle it there, it's easier and cleaner that way.however if you want to got straight from route to view, you better use the below method. put this in your route file

Route::get('/hire-agreement/{param}', function ($param) {
    return view('hire-agreement')->with($param);
});

in the view you can access the param like this

<p>{{$param}}</p>

now if user request "/hire-agreement/1234" your $param in the view will contain 1234, Also if you would like to access get parameters in the url you can do it like this

{{Request::input('q')}}

Upvotes: 2

zahid hasan emon
zahid hasan emon

Reputation: 6233

you don't need anything more in the url section. and to use or get url parameter use laravel request() helper.

$value = request('key');

in view you can print a key like

{{ request('name') }}

complete example for you using request helper

Route::get('/hire-agreement', function () {
    $name = request('name'); //put the key in a variable
    return view('hire-agreement', compact('name')); //send the variable to the view
});

and then in view you can use the variable as

{{ $name }}

if you don't want to use a variable you can use directly request helper in view

{{ request('name') }}

you can use Request class too.

Route::get('/hire-agreement', function (Request $request) {
    $name = $request->name;
    return view('hire-agreement', compact('name'));
});

however i would suggest you to use a controller. don't use closure in route file. u can't cache them when needed.

Upvotes: 2

Kamlesh Paul
Kamlesh Paul

Reputation: 12391

http://example.com/hire-agreement?u=unique_param

in laravel you can access both post and get can be access by Request class instance or request() helper so you can do

with helper request()

Route::get('/hire-agreement', function () {
    dd(request('u')) //  this getting from url ?u=unique_param this u param
    return view('hire-agreement');
});

with Class Request or

Route::get('/hire-agreement', function (Request $request) {
    dd($request->u)) //  this getting from url ?u=unique_param this u param
    return view('hire-agreement');
});

here you can

Upvotes: 2

Related Questions