laravel 5.6销毁功能

时间:2018-06-23 08:12:05

标签: php forms laravel-5

问题是,当我尝试按下按钮时,该按钮应指示我销毁控制器中的功能,以某种方式指示我显示功能

我使用method = delete和route = posts.destroy

                  <tbody>
                          @foreach($MyPosts as $post)
                          <tr>
                            <th scope="row">1</th>
                            <td>{{$post->subject}}</td>
                            <td>{{$post->created_at->toDateString()}}</td>
                            <form method="delete" action="{{route('posts.destroy', $post->id)}}"> 
                            <td> <input class="btn btn-danger"  value="DELETE" type="submit" style="width:100px"></td>
                            </form>
                            @csrf
                            <td><a href="{{route('posts.edit',  $post->id)}}"><input type="submit" class="btn btn-warning" value="edit"></a> </td>
                          </tr>
                          @endforeach
                       </tbody>

路线文件

   Route::get('/Testing', 'ViewsController@getTest');
   Route::get('/contact', 'ViewsController@getcontact');
   Route::get('/about', 'ViewsController@getabout');
   Route::get('/', 'ViewsController@getIndex');
   Route::resource('/posts','PostController');


  public function show($id)
{
        $post = Post::find($id);
        dd($post);
        return view('posts.show')->with('posts',$post);
}


public function destroy($id)
{   
    return('destroy');
}

1 个答案:

答案 0 :(得分:1)

表单方法应为POST,并使用method_field帮助程序:

<td>
<form method="POST" action="{{route('posts.destroy', $post->id)}}"> 
@csrf 
{{ method_field('DELETE') }} 
<input class="btn btn-danger"  value="DELETE" type="submit" style="width:100px">
</form>
</td>