【问题标题】:In laravel The POST method is not supported for this route. Supported methods: GET, HEAD Plz在 laravel 中,该路由不支持 POST 方法。支持的方法:GET、HEAD Plz
【发布时间】:2019-12-28 17:37:47
【问题描述】:

当我更新值时,我是 Laravel 的新手,然后我遇到了这个问题。 Laravel 的消息是

“此路由不支持 POST 方法。支持的方法:GET、HEAD”。

我也使用这些方法(@csrf_field{{ method_field('PUT') }})但结果为零。

HTML 表单

 <form action="{{ $value[0]->id }}" method="post">
     <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">

    <h1>This Is Update Form</h1>

    <table border="1" id="customers">

        <tr>

     <td>Name</td>

     <td>
       <input type="text" name='name' value="{{ $value[0]->name }}">
     </td> 
    </tr>

     <tr>
     <td>Email</td>

     <td>
       <input type="text" name='email' value="{{ $value[0]->email }}">
     </td> 

     </tr>

     <tr>

       <td colspan="2">
         <input type="submit" value="Submit">
       </td>

     </tr>

    </table>
 </form>

控制器

 public function showid($id)
 {
    $data =DB::select('select * from student where id =?',[$id]);
      //DB::table('student')->pluck('id');
    return view('std_edit',['value'=> $data]);
 }

 public function update(Request $request, $id)
 {

    //$data = DB::table('student')
    //->where('id', $data['id'])
    //->update(['name'=>$data['name'], 'email'=>$data['email'] ]);
    $name = $request->input('name');
    $email = $request->input('email');
    DB::update('update student set name = ? email = ? where id = ?',[$name,$email,$id]);
     return redirect()->action('StudInsertController@retrieve');
      echo "Record updated successfully.<br/>";
     //$update = \DB::table('student') ->where('id', $data['id'])->update( [ 'name' => $data['name'],'email' => $data['email'] ]);



 }

路线

 Route::get('edit/{id}','StudInsertController@showid');
 Route::post('post/id','StudInsertController@update');

【问题讨论】:

  • 您在操作中输入了错误的 URL,请检查

标签: php laravel version


【解决方案1】:

表单中的操作是错误的。试试这个:


    <form action="{{ "/post/" . $value[0]->id }}" method="post">

【讨论】:

    【解决方案2】:

    您将错误的路线传递给表单操作。更改您的form action 来自

    <form action="{{ $value[0]->id }}" method="post">
    

    收件人:

    <form action="{{ "/post/" . $value[0]->id  }}" method="post">
    

    编辑:

    刚刚看到您也错误地定义了您的发布路线。改成:

    Route::post('post/{id}','StudInsertController@update');
    

    【讨论】:

      【解决方案3】:

      使用命名路由并为您的路由命名,并且我已经修复了您的路由问题以接受 url 中的参数,我们必须使用{{ param_name}},在您的情况下是{{ id }}

      Route::post('post/{id}','StudInsertController@update')->name('update.student');
      

      然后在你的表单中

      <form action="{{ route('update.student', $value[0]->id) }}" method="post">
         @csrf
      
         /**Your rest of html form*/
      
      </form>
      

      如果你不想使用命名路由,那么你可以使用 laravel 的 url 方法。

       <form action="{{ url('post/'.$value[0]->id) }}" method="post"> @csrf
      
       /**Your rest of html form*/
      
      </form>
      

      谢谢

      【讨论】:

      • @kashif 如果它帮助您将其标记为已接受的答案,以便其他人也将从中受益。谢谢
      • Bro Salman Zafar 非常感谢你再次解决了我的问题
      【解决方案4】:

      在您的表单中,操作 URL 应该是这样的

      <form action="{{ 'post/'.$value[0]->id }}" method="post">
      

      在你的路由文件中

      Route::post('post/{id}','StudInsertController@update');
      

      这将解决您的问题

      【讨论】:

        猜你喜欢
        • 2021-04-08
        • 2020-06-05
        • 2020-04-19
        • 2023-02-05
        • 2021-05-08
        • 2020-01-22
        • 2020-04-25
        • 1970-01-01
        • 2019-10-12
        相关资源
        最近更新 更多