Suhaib Ahmad
Suhaib Ahmad

Reputation: 526

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

Upvotes: 1

Views: 804

Answers (2)

Suhaib Ahmad
Suhaib Ahmad

Reputation: 526

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() }}

Upvotes: 0

Emeka Mbah
Emeka Mbah

Reputation: 17545

Since your URLs are quite similar. How about refactoring your URL.

Also, writing a cleaner code would save you lots of headaches.

At the top:

<?php
use App\Http\Controllers\ModulesController;
use App\Http\Controllers\PathwaysController;

Route::name('modules.')->prefix('modules/courses')->group(function()
    
    Route::get(
        '{course}/edit/pathways/{pathway}/create', //e.g: modules/courses/engligh/edit/pathways/languages/create
        [ModulesController::class, 'create']
    )->name('create'); //modules.create
    
    Route::post(
        '{course}/edit/pathways/{pathway}', 
        [App\Http\Controllers\ModulesController::class, 'store']
    )->name('store'); //modules.store
});

Route::name('courses.')->prefix('courses')->group(function()
    Route::get(
        '{course}/edit/pathways/create', //e.g: courses/english/edit/pathways/create
        [PathwaysController::class, 'create']
    )->name('create'); //courses.create
    
    Route::get(
        '{course}/pathways/{pathway}/edit', 
        [App\Http\Controllers\PathwaysController::class, 'edit']
    )->name('edit');//courses.edit
    
    Route::delete(
        '{course}/pathways/{pathway}', 
        [App\Http\Controllers\PathwayController::class, 'destroy']
    )->name('destroy');//courses.destroy
    
    
    Route::post(
        '{course}/edit/pathways', 
        [App\Http\Controllers\PathwaysController::class, 'store']
    )->name('store');//courses.store
});

Run php artisan route:list to view your routes

Upvotes: 2

Related Questions