#Form performs POST request but Laravel thinks its a GET request!

25 messages · Page 1 of 1 (latest)

formal hollow
#

I have a form that looks like:

                                enctype="multipart/form-data">
                   @csrf
...

And there is a button that triggers the function and it's:

function addLesson(id) {
            const form = document.getElementById('addLessonForm');
            form.action = `/units/${id}/lessons/`;
            SideSection("Adding New Lesson", null, 'addLessonForm');
        }

I get a 405 error message saying method not allowed even though i have similar behavior and functions for units, courses and categories, they work just as fine but it's lessons that causes the issue and it's only happening locally, i have it hosted on AWS LightSail

The routes are:

            Route::get('/units', [UnitController::class, 'index'])->name('unit.index');
            Route::post('/courses/{id}/units', [UnitController::class, 'store'])->name('unit.store');
            Route::get('/courses/{id}/units', [UnitController::class, 'get'])->name('unit.get');
            Route::get('/courses/{id}/units/{unitId}', [UnitController::class, 'show'])->name('unit.show');
            Route::put('/courses/{id}/units/{unitId}', [UnitController::class, 'update'])->name('unit.update');
            Route::delete('/courses/{id}/units/{unitId}', [UnitController::class, 'destroy'])->name('unit.destroy');

                Route::post('/units/{id}/lessons', [LessonController::class, 'store'])->name('lesson.store');
                Route::get('/units/{unitId}/lessons/{lessonId}', [LessonController::class, 'show'])->name('lesson.show');
                Route::put('/units/{unitId}/lessons/{lessonId}', [LessonController::class, 'update'])->name('lesson.update');
                Route::delete('/units/{unitId}/lessons/{lessonId}', [LessonController::class, 'destroy'])->name('lesson.destroy');
                Route::get('/lessons', [LessonController::class, 'index'])->name('lesson.index');
flint venture
#

Why are you adding the action to the form via javascript instead of just putting it in the action attribute of the form?

eager tapir
#

Would be great to see what the network tab shows
Also, if it's local, you added that route recently? Then try php artisan optimize:clear (and never run caching commands locally again)

formal hollow
#

Cause i don't make a dynamic form and the route expects an id that's why I can't

formal hollow
eager tapir
#

Yeah, you told that already. But what's actually happening? There's a request tab, where you can see all the requests. You can see if it first attempts a POST request and then afterwards a GET for example, or if it's immediately making a GET request.

formal hollow
#

Tbh IDK where to find that tab I'm on safari and where exactly i have to locate to find that please?

#

Those are the endpoints i have thought it's better this way, well all the others work just fine but as for adding lessons it's where i can't wrap my head around cause they are similar in terms of functioning

#

@eager tapir @flint venture i have it found, it's a GET request but why though i have the form on POST and i have it checked on logs that it's indeed a POST!

flint venture
#

Do you have any other javascript shenanigans that may be interfering with the form submission?

#

What does that SideSection function even do?

eager tapir
#

Really looks like a redirect. Enable the keep log thingy, so you'll actually see redirects.

#

I guess try to debug the controller method, see if that's being hit first.

formal hollow
# flint venture What does that `SideSection` function even do?

It's a dedicated section for showing forms and that's it, i only have addLesson on a button that returns the lesson as a get request you can see it units route as /courses/id/unjts that's where each unit has their own button to add more lesson into them as follows:

plain ibex
#

Which button is triggering the form submit?

formal hollow
# plain ibex Which button is triggering the form submit?

addLesson makes the form appear in the side section, the. The form has it's own POST method mentioned, when tapped network tab says GET method instead of POST but addLesson is where all the application of action and method is approved

#
function addLesson(id) {
    const form = document.getElementById('addLessonForm');
    form.action = /units/${id}/lessons/;
    form.method = 'POST';

    console.log("Form method before setting:", form.method);
    console.log("Form method after setting:", form.method);

    SideSection("Adding New Lesson", null, 'addLessonForm');
}
#

Both logs are POST

#

The store function for storing lessons is as follows even though it's not reached:

public function store(Request $request, $id)
    {
        // dd($request->all());
        $request->validate([
            'title' => 'required|string|max:255',
            'thumbnail' => 'nullable|image|max:2048',
            'videoUrl' => 'nullable|string|url',
            'pdf' => 'nullable|mimes:pdf|max:2048',
            'free' => 'required|boolean',
            'status' => 'required|boolean'
        ]);

        $data = $request->all();
        $data['unit_id'] = $id;
        $data['views'] = 0;

        if ($request->hasFile('thumbnail')) {
            $data['thumbnail'] = $request->file('thumbnail')->store('lessons', 'public');
        }

        if ($request->hasFile('pdf')) {
            $data['pdf'] = $request->file('pdf')->store('lessons/pdfs', 'public');
        }

        $lesson = Lesson::create($data);

        return redirect()->back()->with('success', 'Lesson added successfully');
    }
plain ibex
#

Right, that updates the form object, though I don't recall if that changes anything in the HTML. But where's the actual submit?

#

<button type='submit'> or form.submit()

formal hollow
formal hollow
#

@plain ibex @eager tapir @flint venture i managed to fix the issue, the endpoint in the form has a / after lessons
Units/id/lessons/

But the endpoint at web.php didn’t have that
Units/id/lessons

Still I don’t get why even that way it worked on local but I’m glad i found the issue.

https://stackoverflow.com/questions/67830649/whys-my-post-request-turning-into-a-get-request-upon-submitting-the-form

The last message made me to check.