Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

laravel - No data is save into my database with a form

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I assume your resource view file directory is admin/recette/admin_validation_ajout_recette

try changing the return view to

return view('admin.recette.admin_validation_ajout_recette');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...