Controller show method intercepting route without parameter

Asked

Viewed 42 times

0

Hello, in my system I have a model Photogallerycategory that has a self relationship. To make my life easier in the create (controller Resource) method, I added a parameter to receive the parent category id. If father is null, it means that that category has no father and it would be root.

I deleted the create method from the Resource route and added a new one by passing the optional parameter to the method. If I enter the route and pass the parameter, it works normally, if I do not pass parameter I am directed to the show method. The /create is understood as a parameter.

Below is my code for this situation:

 Route::resource('/galleryCategory', 'PhotoGalleryCategoryController')->except(['create']);
    Route::get('/galleryCategory/create/{category?}', 'PhotoGalleryCategoryController@create')->name('galleryCategory.create');

public function create($id = null)
    {
        $entity = null;
        $fields = 'app.models.photoGalleryCategory.categoryCreateEdit';
        $action = 'galleryCategory.store';
        $parent = $this->repository->show($id);
        return view('app.templates.form', compact('entity', 'fields', 'action', 'parent'));
    }

Does anyone know how to avoid this problem?

1 answer

0

You are using the verb GET and so is being redirected to the method show, change to POST.

Route::post('/galleryCategory/create/{category?}', 'PhotoGalleryCategoryController@create')->name('galleryCategory.create');
  • then the same thing would not happen but with the store method?

  • Not because the store method by default receives a request as parameter, but the show method receives only an ID which in the case is what you are passing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.