【发布时间】:2020-12-09 15:55:15
【问题描述】:
当我在 laravel 中使用资源控制器时,我无法理解如何调用 destroy 方法。 删除.blade.php
@extends('main')
@section('content')
<form method="POST" action="{{route('posts.destroy', '$post->id') }}" >
@method('DELETE')
@csrf
<select name="id">
<option value="1">vddv</option>
<option value="2">miss</option>
<option value="3">miss</option>
<option value="4">joy</option>
</select>
<br><br>
<button type="submit"> Delete blog</button>
</form>
@endsection
资源控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\posts;
use Sessions;
class PostController extends Controller
{
public function create()
{
return view('posts.create');
}
public function store(Request $request)
{
$post = new posts;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();
return redirect('posts/read');
}
public function show($data)
{
echo "show";
}
public function edit($id)
{
return view('posts.edit');
}
public function update(Request $req, $id)
{
echo posts::where('title' , $req->title)
->update(['body'=>$req->body]);
return redirect('/');
}
public function destroy($id)
{
$post = posts::find($id);
$post->delete();
return redirect('/');
}
}
路线:
Route::resource('posts', 'PostController');
在 GET 请求通过时调用 show 方法。请指导我如何调用销毁方法。如文档中所述,我使用表单方法欺骗传递 @method('DELETE'),因为 html 仅识别 GET 和 POST 方法。
【问题讨论】:
-
您不能以这种方式删除表单。你只能这样做:stackoverflow.com/questions/46098806/…
-
可能不相关,但你拼错了,把
{{route('posts.destroy', '$post->id') }}改成{{route('posts.destroy', $post->id) }} -
在
public function destroy($id)中更改$post = Post::find($id);而不是$post = posts::find($id);并在控制器中更改use App\Post;而不是use App\posts;
标签: php laravel laravel-5 backend laravel-7