Method validate does not exist - Laravel 5.4

2020-02-27 03:58发布

I have a very weird problem. When I'm submitting the form, it throws an error with server-side validation.

Here is my simple controller:

namespace App\Http\Controllers;

use Newsletter;
use Illuminate\Http\Request;

class SubscriptionController extends Controller
{
    public function subscribe(Request $request)
    {
        $request->validate([
            'email' => 'required|email',
            ]);
    }
}

Submitting the form gives me:

BadMethodCallException Method validate does not exist.

it should work according to:

https://laravel.com/docs/5.4/validation

7条回答
等我变得足够好
2楼-- · 2020-02-27 04:02

Actually If you add the right controller, validate method should be already included. You can try adding below controller.

Instead: use App\Http\Controllers\Controller;

查看更多
在下西门庆
3楼-- · 2020-02-27 04:08

lets add these two packages Best of luck

use Illuminate\Support\Facades\Validator; use Illuminate\Foundation\Auth\RegistersUsers;

查看更多
SAY GOODBYE
4楼-- · 2020-02-27 04:12

Well means its no longer available in 5.4 however its available in controller

Try:

 $this->validate($request, [
    'email' => 'required|email',
 ]);
查看更多
地球回转人心会变
5楼-- · 2020-02-27 04:14

You can use the Validator service provider.

     namespace App\Http\Controllers;

     use Newsletter;
     use Illuminate\Http\Request;
     use Validator;

     class SubscriptionController extends Controller
     {
          public function subscribe(Request $request)
          {
                   $request->validate($request->all(),[
                     'email' => 'required|email',
                  ]);
           }
查看更多
Viruses.
6楼-- · 2020-02-27 04:15
        $validator = \Validator::make($request->all(), [
            'mobile_number' => 'required',]);

        if ($validator->fails()) {
            return redirect()->back()
            ->withErrors($validator)
            ->withInput();
        }

Hope this works for you..

查看更多
该账号已被封号
7楼-- · 2020-02-27 04:21

In docs said:

$this->validate($request, [
    'email' => 'required|email',
]);

This string - works :)

查看更多
登录 后发表回答