我正在使用 Laravel 5.7 并在验证我的表单字段时收到此错误:
“方法 App\Http\Controllers\todocontroller::validate 不存在错误。”
这是我的控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\todo;
class todocontroller extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$todos= todo::all();
return view('todo.home',compact('todos'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('todo.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$todo = new todo;
$this->validate($request,[
'body'=>'required',
'title'=>'required|unique:todos',
]);
$todo->body = $request->body;
$todo->title = $request->title;
$todo->save();
return redirect('/todo');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$todo=todo::find($id);
return view('todo.show',compact('todo'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$todo=todo::find($id);
return view('todo.edit',compact('todo'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$todo = todo::find($id);
$this->validate($request,[
'body'=>'required',
'title'=>'required',
]);
$todo->body = $request->body;
$todo->title = $request->title;
$todo->updated_at = now();
$todo->save();
session()->flash('message','Updated Successfully!');
return redirect('/todo');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$todo = todo::find($id);
$todo -> delete();
session()->flash('message','Deleted Successfully!');
return redirect('/todo');
}
}
错误在此代码中
$this->validate($request,[
'body'=>'required',
'title'=>'required|unique:todos',
]);
但根据 laravel 文档,我的验证方法是正确的。请让我知道我在控制器中做错了什么。我也用谷歌搜索了这个错误,但没有成功。
我也尝试过使用这些行:
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Validator;
但没有成功。请帮助我,因为我在过去 10 天内收到此错误。我不明白现在该怎么办。您的帮助将不胜感激。谢谢。