Ahmad Hilman
Ahmad Hilman

Reputation: 103

Passing parameter using routes laravel 5.8

I have two routes

Route::get('/fasilitas/{fasilitas_id}/{any}','Fasilitas_controller@detail_fasilitas');
Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller@get_kategori');

but when i access the second routes, it always gets the first routes why? and how to fix it? thanks for helping me

Upvotes: 4

Views: 330

Answers (4)

Ripon Uddin
Ripon Uddin

Reputation: 714

Try with name . It will be more efficient
Fasilitas !
May be you tried to write facilities. Whatever, am writing base on ur method.

Route::get('/fasilitas/{fasilitas_id}/{any}','Fasilitas_controller@detail_fasilitas')->name('detail.fasilitas');
Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller@get_kategori')->name('get.kategori');

How to call at front page for two parameter and single parater ? Have a look below .

For detail.fasilitas

<a href="{{ route('detail.fasilitas',$fasilitas_id,$any) }}">Detail Fasilitas</a>

For Single Parameter : get.kategori

<a href="{{ route('get.kategori',$kf_id) }}">Get Kategori</a>

Try it.
Let me know its work or not .
Have fun with code.

Upvotes: 0

Dilip Hirapara
Dilip Hirapara

Reputation: 15316

You may constrain the format of your route parameters using the where method on a route instance. The where method accepts the name of the parameter and a regular expression defining how the parameter should be constrained:

Route::get('/fasilitas/{fasilitas_id}/{any}','Fasilitas_controller@detail_fasilitas')->where(['fasilitas_id' => '[0-9]+', 'any' => '[0-9]+']);
Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller@get_kategori')->where('kf_id' => '[0-9]+');

For more info Regular Expression Constraints

Another way to pass it define name route.

Route::get('/fasilitas/{fasilitas_id}/{any}','Fasilitas_controller@detail_fasilitas')->name('fasilitas.example1');

  <a href="{{ route('fasilitas.example1',['fasilitas_id'=>1,'any'=>2]) }}">

  Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller@get_kategori')->name('fasilitas.example2');

  <a href="{{ route('fasilitas.example2',['kf_id'=>1]) }}">

Upvotes: 1

smartrahat
smartrahat

Reputation: 5659

The second segment of the first route is a wildcard, that means it could be anything.

When the second route is called in browser the second segment (/get_kategori/) is passing through the wildcard of the first route.

Changing the route order may solve the problem. But the best practice is changing the route name. Example:

Route::get('/fasilitas/something_else/{fasilitas_id}/{any}','Fasilitas_controller@detail_fasilitas');
Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller@get_kategori');

Upvotes: 0

user8034901
user8034901

Reputation:

When hitting /fasilitas/get_kategori you trigger the first route, with get_kategori being the {fasilitas_id}.

Change the order of your routes, so /fasilitas/get_kategori gets triggered first:

Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller@get_kategori');
Route::get('/fasilitas/{fasilitas_id}/{any}','Fasilitas_controller@detail_fasilitas');

Upvotes: 1

Related Questions