Ahmed Rafie
Ahmed Rafie

Reputation: 101

optional additional parameters on resource route - laravel

I had searched for how to add parameters to resource route

Route::resource('posts','PostsController');
// became
Route::resource('posts/category.post','PostsController');

now , by the category.post I can declare additional parameters to all resource routes

but they are required , my question is how to make them optional ?

I tried something like this

Route::resource('posts/category?.post','PostsController');

to make the category parameter be an optional one , but that didn't work with me .

how I can do so ?

thank you .

Upvotes: 4

Views: 1291

Answers (2)

Jamil Shafayat
Jamil Shafayat

Reputation: 97

You can try this, not sure though..

Route::resource('posts', 'PostsController')->except(['store' ]);   
Route::post('posts/category', 'PostsController@store');

Upvotes: 2

Jasper Helmich
Jasper Helmich

Reputation: 751

Resource route is not just a "route"

You may see it as a route group, but it is predefined and can easily be implemented when you have a normal resource controller

If you want to change the parameters you'll have to define the routes individually

Then you can make the parameters optional as needed

Route::post('/posts/category/{post?}, 'PostsController@store');

See the following docs

https://laravel.com/docs/7.x/routing#parameters-optional-parameters

Upvotes: 1

Related Questions