Laravel-modules: Method validate does not exist.

Created on 13 Nov 2017  路  12Comments  路  Source: nWidart/laravel-modules

Here is my controller

<?php
namespace Modules\Admin\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;


class AdminLoginController extends Controller
{
    public function __construct()
    {
        $this->middleware('guest:admin');
    }

    public function showLoginForm()
    {
        return view('admin::login');
    }

    public function login(Request $request)
    {
        //validate form data
        $this->validate($request, [
            'email' => 'required|email',
        ]);

        //attempt to log the user in
        if(Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)){
            return redirect()->intended(route('admin.dashboard'));
        }
    }

The error I am getting is "Method [validate] does not exist."
Does anyone faced the same issue? Please share the solution.

Most helpful comment

That's because you're not importing the trait. Please refer to the laravel/php docs.

Example here: https://github.com/laravel/laravel/blob/master/app/Http/Controllers/Controller.php#L12

All 12 comments

You need to import the trait to validate

Illuminate\Foundation\Validation\ValidatesRequests;

Hi Nicolas Widart,
I imported the trait, but did not work. What wrong am I doing, Below is my code:

<?php
namespace Modules\Admin\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;
Illuminate\Foundation\Validation\ValidatesRequests;

class AdminLoginController extends Controller
{
     public function login(Request $request)
    {
        //validate form data
        $this->validate($request, [
            'email' => 'required|email',
        ]);
    }
}

Also I tried to impport within the class like:

<?php
namespace Modules\Admin\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;

class AdminLoginController extends Controller
{
Illuminate\Foundation\Validation\ValidatesRequests;
     public function login(Request $request)
    {
        //validate form data
        $this->validate($request, [
            'email' => 'required|email',
        ]);
    }
}

That's because you're not importing the trait. Please refer to the laravel/php docs.

Example here: https://github.com/laravel/laravel/blob/master/app/Http/Controllers/Controller.php#L12

I am very sorry @nWidart,
I did not understand, My main controller inside app/Http/Controller is same as: https://github.com/laravel/laravel/blob/master/app/Http/Controllers/Controller.php#L12

and you're extending use Illuminate\Routing\Controller;... please, read the documentation.

Thanks @nWidart, understood now, working well now.
Cheers!

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Illuminate\Support\Fascades\DB;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

Already add the use Illuminate\Foundation\Auth\Access\AuthorizesRequests; but still get the error.
When i use the use Validator; still get the error

@Ramonivandy, replace 'use Illuminate\Routing\Controller;' with 'use App\Http\Controllers\Controller;'
because 'use Illuminate\Routing\Controller' might have already added in '\App\Http\Controllers\Controller.php'.
Hope this will solve the issue.

Then my question is: why the controllers under nWidart modules not using 'use App\Http\Controllers\Controller;' by default ?

Shouldn't it be this way by default? validation etc are very common use then why need to import it explicitly and replace the controller?

and you're extending use Illuminate\Routing\Controller;... please, read the documentation.

sorry, but i must say. above elaborates nothing until i read the last reply from @bhattraideb. I am still wondering how did he got you?

Good question from @amitshahc. @nWidart it would be great if you can override 'use Illuminate\Routing\Controller;' to 'use App\Http\Controllers\Controller;' by default when creating a controller.

@Ramonivandy, replace 'use Illuminate\Routing\Controller;' with 'use App\Http\Controllers\Controller;'
because 'use Illuminate\Routing\Controller' might have already added in '\App\Http\Controllers\Controller.php'.
Hope this will solve the issue.

If you want to have use App\Http\Controllers\Controller; by default in your project you can change stub files, wich located in:
\vendor\nwidart\laravel-modules\src\Commands\stubs\controller-api.stub
\vendor\nwidart\laravel-modules\src\Commands\stubs\controller-plain.stub
\vendor\nwidart\laravel-modules\src\Commands\stubs\controller.stub

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kamov picture kamov  路  10Comments

jakecorn picture jakecorn  路  16Comments

elephantux picture elephantux  路  11Comments

designvoid picture designvoid  路  10Comments

bashet picture bashet  路  19Comments