Search code examples
laravelroutescontrollerroute-parameters

laravel route::post wrong method gets called


Starting off with a bit of background information, i have 3 models - Course, Pathway, Module.

Course HAS-MANY Pathway
Pathway HAS-MANY Module
Course HAS-MANY-THROUGH Module (through Pathway)

I have set up routes for creating Course, Pathway and Module. However, when I try to save the newly created model instance, it calls the wrong route method - does not even hit the store method of the relevant Controller

I understand that the order of the routes is important. I tried changing them around but it still does not work as intended.

Here's what I have so far :

// modules
Route::get('/courses/{course}/edit/pathways/{pathway}/modules/create', [App\Http\Controllers\ModulesController::class, 'create'])->name('createModule');
Route::post('/courses/{course}/edit/pathways/{pathway}/modules', [App\Http\Controllers\ModulesController::class, 'store'])->name('storeModule');

// Pathways
Route::get('/courses/{course}/edit/pathways/create', [App\Http\Controllers\PathwaysController::class, 'create'])->name('createPathway');
Route::get('/courses/{course}/pathways/{pathway}/edit', [App\Http\Controllers\PathwaysController::class, 'edit'])->name('editPathway');
Route::delete('/courses/{course}/pathways/{pathway}', [App\Http\Controllers\PathwayController::class, 'destroy'])->name('destroyPathway');
Route::post('/courses/{course}/edit/pathways', [App\Http\Controllers\PathwaysController::class, 'store'])->name('storePathway');


// VQs/Qualifications
Route::resource('courses', App\Http\Controllers\CourseController::class, [
    'names' => [
        'index'     => 'allCourses',
        'create'    => 'createCourse',
        'store'     => 'storeCourse',
        'show'      => 'showCourse',
        'edit'      => 'editCourse',
        'update'    => 'updateCourse',
        'destroy'   => 'destroyCourse',
    ]
]);

The problem is that when I try to store a Pathway or Module, it hits the Route::post('/courses/{course}') route.

I tried changing around the order of the routes, but none of that worked. I've also made sure that the create forms action is of the right Url Route. its all still the same.

I also can't tell which controller method is being called. Tried doing a dd() on CourseController@create, PathwaysController@create, ModulesController@create but none of them get hit.

Any help as to why this is happening will be greetly appreciated


Edit

here are some of my routes: enter image description here


Solution

  • Fixed it. Turns out there wasn't a problem with my routes at all.

    The problem was that I had vertical navs and tab-panes on that page, and most of them had a form in them. I had not closed the form in one of the tab-panes and so this form was getting submitted to the action of the form above in that page.

    i.e. Make sure to close forms using:

    {{ Form::close() }}