Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

optional additional parameters on resource route - laravel

Tags:

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 .

like image 960
Ahmed Rafie Avatar asked Oct 27 '25 10:10

Ahmed Rafie


2 Answers

You can try this, not sure though..

Route::resource('posts', 'PostsController')->except(['store' ]);   
Route::post('posts/category', 'PostsController@store');
like image 197
Jamil Shafayat Avatar answered Oct 28 '25 23:10

Jamil Shafayat


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

like image 35
Jasper Avatar answered Oct 29 '25 01:10

Jasper



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!