【问题标题】:Page 404 not found laravel 8页面404找不到laravel 8
【发布时间】:2021-07-29 06:41:44
【问题描述】:

当我点击提交按钮时,我正在尝试在数据库中插入数据,它返回 404 not found

这里是控制器

   function ajouterfrm()
   {
    return view('dashboards.directeur.ajouter');
   }
   
   function add(Request $request)
   {
        $request->input();  
   }

这里是路线

Route::get('rapport/ajouter',[DirecteurController::class,'ajouterfrm'])->name('directeur.ajouter');
Route::post('add',[DirecteurController::class,'add'])->name('directeur.add');

我的 html : 这是我的html

<div class="container">
    <div class="row">
        <div class="col-md-6 col-md-offset-3" style="margin-top: 50px">
            <h4>Ajouter Rapport</h4>
            <hr>
            <form action="add" method="POST">
               <input type="text" name="data_1" required>
               <input type="text" name="data_2" required>
               <!-- others inputs and selects ... -->
            </form>
        </div>
    </div>
</div>

我试图取消最后一条路线的名称,但没有任何结果

有什么建议吗?

谢谢

【问题讨论】:

  • 所以我猜第一条路线rapport/ajouter 工作正常吗?
  • 是的,它的工作问题是当我点击提交按钮时
  • 你提供的代码是整个代码吗?正如$request-&gt;input() 在这里并没有真正做任何事情
  • 仅供测试

标签: php laravel laravel-8


【解决方案1】:

您的表单应如下所示:

<form action="{{ route('directeur.add') }}" method="post">
  @csrf // for preventing forgery token attacks otherwise you will get an "419 error"
  <your inputs ...>
<form>

好机会。

【讨论】:

    【解决方案2】:

    此时您的添加路由不执行任何操作,因此不会为请求返回视图或响应。

    你需要从你的控制器返回一些东西。请看:

    https://laravel.com/docs/8.x/responses

    但是现在你可以试试你的添加功能

    return response('test');
    

    【讨论】:

    • 我忘了写return语句,但它仍然
    • ` function add(Request $req){ return $req->input(); } `
    【解决方案3】:

    这是我的html

    <div class="container">
        <div class="row">
            <div class="col-md-6 col-md-offset-3" style="margin-top: 50px">
    
                <h4>Ajouter Rapport</h4>
    
                <hr>
    
                <form action="add" method="POST">
    
                
                    <div class="form-group">
                      <label for="">Titre</label>
                      <input type="text" name="titre" class="form-control" placeholder="Entrer le titre du rapp">
                    </div>
    
                    <div class="form-group">
                        <label for="">Description</label>
                        <textarea name="description" class="form-control" rows="3" placeholder="Entrer la description ici...">
                        </textarea> 
    
                    </div>
    
                    <div class="form-group">
                          <label for="">Médecin</label>
                          <select class="form-control" name="medecin" id="">
                            <option value="med">Médecin 1</option>
                            <option value="med">Médecin 2</option>
                            <option value="med">Médecin 3</option>
                          </select>
    
    
                        </div>
                    
                     <div class="form-group">
                       <label for="">Patient</label>
                       <select class="form-control" name="patient" id="">
                         <option value="pat">Patient 1</option>
                         <option value="pat">Patient 2</option>
                         <option value="pat">Patient 3</option>
                       </select>
                    </div>
                    <br>
                     <button type="submit" class="btn btn-primary btn-block">Ajouter</button>
                </form>
            </div>
        </div>
    </div>
    

    【讨论】:

    • 只需删除这段代码,我已将其添加到您的答案中。要联系用户,您可以直接在他的个人资料页面上找到他的电子邮件/联系人。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 1970-01-01
    • 2016-10-20
    • 2021-08-06
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多