Hariharan Thiagarajan
Hariharan Thiagarajan

Reputation: 65

Laravel Route Name based on parameter passed

This is how my Laravel route is present

Route::match(['get', 'post'], 'add-edit-category/{id?}', 'CategoryControlller@addEditCategory')->name('Add/Edit Category');

So if I get an ID - then the controller invokes the Edit function else it does create function.

How to do the ->name() condition so that, if I have a variable I send ->name('Add Category') and if not ->name('Edit Category')

Tried Googling, but couldn't seem to find an example for the same.

Upvotes: 1

Views: 888

Answers (2)

Faizan Ali
Faizan Ali

Reputation: 330

Use ternary operator for this purpose:

Pass flag from add category controller function to blade:

$add_catrgory = true;

then in blade you can check in this way:

href= "{{$add_category == true ? route('Add_Category') : route('Edit_Category', $id ) }}"

Upvotes: 0

elias.xe
elias.xe

Reputation: 57

Just use 2 routes, one with id and one without like:

Route::match(['get', 'post'], 'add-edit-category', 'CategoryControlller@addEditCategory')->name('Add_Category');
Route::match(['get', 'post'], 'add-edit-category/{id}', 'CategoryControlller@addEditCategory')->name('Edit_Category');

Upvotes: 3

Related Questions