1

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

Suhaib Ahmad
  • 487
  • 6
  • 25

2 Answers2

2

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

Emeka Mbah
  • 16,745
  • 10
  • 77
  • 96
  • Thanks tried it, but still doesnt fix my issue. The `store` method for both `Pathways` and `Modules` leads to `courses/{course}`, with a blank page – Suhaib Ahmad Jan 21 '22 at 16:40
  • Can you paste the url for both Pathways and Modules here – Emeka Mbah Jan 21 '22 at 16:42
  • Does the **store** method get hits? do you have other conflicting routes above these? Do you have a middleware in your controller construct that redirects? – Emeka Mbah Jan 21 '22 at 16:43
  • `/modules/courses/21/edit/pathways/4/create` returns form to create module. on submit, the url becomes `/courses/21` (you might see here that module comes before courses, which isnt what I want but doesn't matter atm. Similarly, `/courses/21/edit/pathways/create`, returns form to create pathway, on submit the url becomes `/courses/21` – Suhaib Ahmad Jan 21 '22 at 16:51
  • No the store method is not being hit. Only have the Auth middleware. I dont believe there are any more conflicting routes, the `courses` `pathways` and `modules` are the ones already mentioned in the question – Suhaib Ahmad Jan 21 '22 at 17:00
  • Where is your request coming from? API or a form. If form, please share your blade form. Are you using the right action URL? Is this an ajax request? `{{route('modules.store')}}`, can you share your `php artisan route:list` ? – Emeka Mbah Jan 22 '22 at 07:14
  • the request is coming from a form, and I've set the right action URL using named route. However, when i inspect or view the page source, I cannot see the `
    ` element with the action attribute. Will edit the question to include the form and the routes
    – Suhaib Ahmad Jan 24 '22 at 10:59
0

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() }}
Suhaib Ahmad
  • 487
  • 6
  • 25