I have a form where I put 3 information in it and a button submit. I complete the form and when I click on add, it redirect me into the main page, even if I return a view
different from the one where it redirect me.
Here is my form :
<form class="mt-3" method="POST" id="formCreation" action="{{ route('admin.liste.recette.ajouter.recette') }}">
@csrf
<input type="text" class="form-control" id="titre" name="titreRecette" value="" required>
<select id="inputState" class="form-control" name="typeRecette" >
<option selected></option>
@foreach ($type_recettes as $type_recette)
<option value="{{ $type_recette->id_type_recette }}">{{ $type_recette->libelle }}</option>
@endforeach
</select>
<select id="inputState" class="form-control" name="saisonRecette" >
<option selected></option>
@foreach ($saisons as $saison)
<option value="{{ $saison->id_saison }}">{{ $saison->libelle }}</option>
@endforeach
</select>
<button type="submit" form="formCreation" class="btn my-4" style="color:white; background-color: #369d4a;">Add</button>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</form>
My controller fuction to add an element :
public function creer_recette(Request $request)
{
$data = $request->validate([ // $data = $this->validate($request
'titreRecette' => 'bail|required|between:5,40',
'typeRecette' => 'bail|required',
'saisonRecette' => 'bail|required'
]);
$recette = new Recette();
$recette->titre = $data['titreRecette'];
$saison_recette = Saison::findOrFail($data['saisonRecette']);
$type_recette = Type_recette::findOrFail($data['typeRecette']);
$recette->saison()->associate($saison_recette);
$recette->type_recette()->associate($type_recette);
$recette->save();
return view('admin/recette/admin_validation_ajout_recette');
}
And my web.php :
Route::group(['prefix' => 'liste-recette'], function(){
Route::post('creation_recette', [RecetteController::class, 'creer_recette'])->name('admin.liste.recette.ajouter.recette');
});
No error displaying, just a redirection to the main page (I've gota function + a route for it)
Cordially
EDIT : The error came from some comments I add to my validate tab, Laravel didn't take it as comments and I don't know why...
question from:
https://stackoverflow.com/questions/65943465/no-data-is-save-into-my-database-with-a-form 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…