Home > Software engineering >  Laravel : form does not change the URL after submit, causing me to be unable to do another POST in w
Laravel : form does not change the URL after submit, causing me to be unable to do another POST in w

Time:01-19

I have a bit of a complicated issue. I could use some help.

I have a form that is being handled by the following function:

$module = request('module');
        $classe = request('classe');
        $horaire = request('horaire');
        $date = request('date');
        $students = DB::select('SELECT * FROM `etudiants` WHERE etudiants.id_classe = '.$classe);
        return view('g_absence.absence',['module'=> $module, 'classe'=>$classe,'horaire'=>$horaire,'date'=>$date,'students'=>$students]);

I take the values $module, $class, $horaire, $date and $students and need to use them inside a different view: g_absence.absence. This works fine and when the view is returned I have access to said variables.

The issue is, inside the g_absence.absence view, I have another form that also needs to be handled, and because the url remains the same even tho a different view is returned, I cant make two posts for the same path.

web.php:

    Route::get('/testboy', [App\Http\Controllers\g_absence::class,'index'])->name('marquer');
    Route::post('/testboy',[App\Http\Controllers\g_absence::class, 'marquer']);
    Route::post('/testboy',[App\Http\Controllers\g_absence::class, 'ajoutabsence']);
  • The first line is the one that send to the form page just a simple return view
  • The second one handle the form in that view
  • The third one, I want it to handle the form inside the g_absence.absence view, but they share the same path.

Excuse me if I'm being unclear, I'm a bit of a beginner in Laravel

CodePudding user response:

your problem is using the same route for different methods basically the first route gets executed every time you use the '/testboy' action that is why your second function never get's called.

you can solve this issue by changing your urls for example:

Route::post('/testboy-marquer',[App\Http\Controllers\g_absence::class, 'marquer']);
Route::post('/testboy-ajoutabsence',[App\Http\Controllers\g_absence::class, 'ajoutabsence']);

Or you can use one function that's handle both with one url by pathing additional parameter to your url depending on your function call :

Route::post('/testboy?type=marquer',[App\Http\Controllers\g_absence::class, 'ajoutabsence']);

in your function check the type :

if(request('type') == 'marquer') {
 execute absence logic here...
} else {
 execute marquer logic here...
}

CodePudding user response:

Using method and path with two functionalities is wrong, but if you want to somehow use both routes my same method and path which I don't recommend you must let the request to pass through like a middleware in your first block of code Instead of return a view.

Recommended way is to have 2 routes with different paths or at least one route with a parameter by which you can determine what code block must be executed.

  •  Tags:  
  • Related