2

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

4

4 回答 4

2

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

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

<?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文件,确保它看起来像上面这样。

于 2018-09-22T12:20:51.907 回答
1
$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');

如果验证失败,将自动生成正确的响应。如果验证通过,我们的控制器将继续正常执行。

再次检查在文档中编写验证逻辑

于 2018-09-22T11:10:39.347 回答
0

尝试这个 :

use Validator;
....
....

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

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

于 2018-09-22T11:10:19.383 回答
0

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

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

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

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

于 2021-04-15T20:50:09.410 回答