此路由不支持 PUT 方法。支持的方法:GET、HEAD、POST Laravel 6
PUT method not supported for this route. Supported methods: GET, HEAD, POST Laravel 6
我正在使用 Laravel 6 制作编辑表单。显然,这是一个普遍存在的问题,我在这里查找如何解决它,我尝试以 5 种方式放置隐藏的 csrf 字段,但我每次 运行 都出现相同的错误,所以如果不推荐使用这些解决方案,请 IDK Laravel 6 或者我做错了什么。
edit.blade.php
<form method="POST" action="/posts/{{$post->edit}}" enctype="multipart/form-data">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label for="exampleFormControlInput1">Email address</label>
<input type="email" name="email" value="{{ $post->email }}" class="form-control"
id="exampleFormControlInput1">
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Name</label>
<input type="text" name="name" value="{{ $post->name }}" class="form-control"
id="exampleFormControlInput2" placeholder="Name">
</div>
<label for="exampleFormControlInput1">Image</label>
<div class="form-group row">
<div class="col-sm-2">
@if($post->image)
<img class="img-fluid card-img-top" src="/images/{{ $post->image }}"/>
@endif
</div>
<input type="file" name="image" value="{{ $post->image }}"
id="exampleFormControlInput3">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
PostsController.php
public function edit(Post $post)
{
return view ('posts.edit', compact('post'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Post $post)
{
$post->update($request->all());
$post->name = $request->name;
$post->email = $request->email;
if(Input::hasFile('image')){
$file = Input::file('image');
$path = time().$file->getClientOriginalName();
$destinationPath = public_path(). '/images/';
$filename = time().$file->getClientOriginalName();
$file->move($destinationPath, $filename);
//then proceeded to save
$post->image = $destinationPath.$filename;
$post->save();
}
else
$post->save();
return redirect('posts.all');
}
我的路线,以备不时之需
Route::resource('posts', 'PostsController');
这些是我尝试写csrf字段的其他方法。
方式一:
{{ csrf_field() }}
<input type="hidden" name="_method" value="PUT">
方式二:
@csrf_field
{{ method_field('PUT') }}
方式三:
@csrf
{{ method_field('PATCH') }}
方式四:
@csrf
@method('PUT')
所有这些都导致我收到相同的错误消息。
尝试替换这个:
<form method="POST" action="/posts/{{$post->edit}}"
用这个:
<form method="POST" action="{{ route('posts.update', [$post->id]) }}"
我认为您正在尝试 Post 在 /posts/ 路由上不带任何参数,因为 $post->edit 可能不会 return id post :)
我正在使用 Laravel 6 制作编辑表单。显然,这是一个普遍存在的问题,我在这里查找如何解决它,我尝试以 5 种方式放置隐藏的 csrf 字段,但我每次 运行 都出现相同的错误,所以如果不推荐使用这些解决方案,请 IDK Laravel 6 或者我做错了什么。
edit.blade.php
<form method="POST" action="/posts/{{$post->edit}}" enctype="multipart/form-data">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label for="exampleFormControlInput1">Email address</label>
<input type="email" name="email" value="{{ $post->email }}" class="form-control"
id="exampleFormControlInput1">
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Name</label>
<input type="text" name="name" value="{{ $post->name }}" class="form-control"
id="exampleFormControlInput2" placeholder="Name">
</div>
<label for="exampleFormControlInput1">Image</label>
<div class="form-group row">
<div class="col-sm-2">
@if($post->image)
<img class="img-fluid card-img-top" src="/images/{{ $post->image }}"/>
@endif
</div>
<input type="file" name="image" value="{{ $post->image }}"
id="exampleFormControlInput3">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
PostsController.php
public function edit(Post $post)
{
return view ('posts.edit', compact('post'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Post $post)
{
$post->update($request->all());
$post->name = $request->name;
$post->email = $request->email;
if(Input::hasFile('image')){
$file = Input::file('image');
$path = time().$file->getClientOriginalName();
$destinationPath = public_path(). '/images/';
$filename = time().$file->getClientOriginalName();
$file->move($destinationPath, $filename);
//then proceeded to save
$post->image = $destinationPath.$filename;
$post->save();
}
else
$post->save();
return redirect('posts.all');
}
我的路线,以备不时之需
Route::resource('posts', 'PostsController');
这些是我尝试写csrf字段的其他方法。
方式一:
{{ csrf_field() }}
<input type="hidden" name="_method" value="PUT">
方式二:
@csrf_field
{{ method_field('PUT') }}
方式三:
@csrf
{{ method_field('PATCH') }}
方式四:
@csrf
@method('PUT')
所有这些都导致我收到相同的错误消息。
尝试替换这个:
<form method="POST" action="/posts/{{$post->edit}}"
用这个:
<form method="POST" action="{{ route('posts.update', [$post->id]) }}"
我认为您正在尝试 Post 在 /posts/ 路由上不带任何参数,因为 $post->edit 可能不会 return id post :)