方法 App\Http\Controllers\todocontroller::validate 在 laravel 中不存在错误

Method App\Http\Controllers\todocontroller::validate does not exist error in laravel

我正在使用 Laravel 5.7,在验证我的表单字段时出现此错误:

"Method App\Http\Controllers\todocontroller::validate does not exist error."

这是我的控制器:

<?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 天内收到此错误。我不明白现在该怎么办。您的帮助将不胜感激。谢谢。

试试这个:

use Validator;
....
....

Validator::make($request->all(), [
    'title' => 'required|unique:posts|max:255',
    'body' => 'required',
])->validate();

link : https://laravel.com/docs/5.7/validation#automatic-redirection

$validatedData = $request->validate([
    'body'=>'required',
    'title'=>'required|unique:todos',
]);

$todo = new todo;
$todo->body = $request->body;
$todo->title = $request->title;
$todo->save();

return redirect('/todo');

If the validation fails, the proper response will automatically be generated. If the validation passes, our controller will continue executing normally.

再次检查文档中的 Writing The Validation Logic

尽管其他答案可能包含有效的替代方法,$this->validate(...) 应该可以在您的控制器中正常工作。但是你应该确保你没有改变标准 Laravel 基本控制器的任何内容。

位于 here 的默认控制器如下所示:

<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}

如您所见,它包含 ValidatesRequests 特征,并且该特征提供 validate 方法。因此,请查看您的 app/Http/Controllers/Controller.php 文件并确保它看起来像上面这样。

我在尝试像这样使用它时遇到了问题

$this->validate(request(), [
        "title" => "required",
     
    ]);

在那里犯了一个愚蠢的错误。

确保从正确的位置使用控制器。 使用 App\Http\Controllers\Controller;