Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5: build URL with custom parameters

Tags:

php

laravel

I have an application in which you can choose between multiple customers. Choosing a customer will generate following URL:

http://localhost:8000/customer/CUSTOMER_NAME

From there on, I would like to choose a specific sub-page (e.g.: the support page)

How do I generate the following link:

http://localhost:8000/customer/CUSTOMER_NAME/support

So far, I always lose my CUSTOMER_NAME parameter, and I do not know how to keep it.

The framework I use is Laravel 5.

Any ideas?

like image 736
Thomas Vercamer Avatar asked Dec 27 '25 19:12

Thomas Vercamer


1 Answers

You shall do this by passing the url param to the view by

I believe you have something like this in your route

Route::get('customer/{id}', 'yourController@yourFunctionName');

Then, in your controller, you may have

public function yourFunctionName($id)
{
    return view('yourViewName')->with('id', $id);
}

Then from your view can simply do this to generate a url like this

<a href="customer/{id}/support">Click here</a>

To have the url like below

http://yourprojectname/customer/18/support

Advice : Use the Primary key or any unique field rather than using name to avoid some future issues.

Also you shall use helpers to generate url's

like image 97
Sulthan Allaudeen Avatar answered Dec 30 '25 09:12

Sulthan Allaudeen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!