我正在尝试使用laravel雄辩的更新方法,并且出现此错误非静态方法
不应静态调用非静态方法Illuminate \ Database \ Eloquent \ Model :: update()
View
@extends('posts.layout')
@section('content')
<form action="/posts/{{ $post->id }}" method="post">
@method('PATCH')
@csrf
<input type="text" name="title" placeholder="{{ $post->title }}">
<textarea name="body" placeholder="{{ $post->body }}"></textarea>
<button type="submit">Edit</button>
</form>
@endsection
Controller Methods
public function edit(Post $post)
{
return view('posts.edit', compact('post'));
}
public function update(Request $request, Post $post)
{
post::update(request(['title', 'body']));
return redirect('/posts');
}
答案 0 :(得分:1)
使用Dependency Injection中的$post
模型对象对该对象执行更新
public function update(Request $request, Post $post)
{
$post->title = $request->title;
$post->body = $request->body;
$post->save();
return redirect('/posts');
}
希望这会有所帮助