1

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.

1

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, could you tell me where I would put the $value = request('key'); I dont understand why I need this line?
this is just an example. you can use this in view as well as in controller. here i am just putting the request key in a variable.
2

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

Comments

2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.