jzmwebdevelopment's avatar

Call to undefined method

I have a protected function that I want to use to check my login validation. However I am getting the error

Call to undefined method Illuminate\Database\Query\Builder::loginValidation

Do I have to write a model for my function?

Login Validation:

    protected function loginValidation($data)
    {
        $rules = array(
          'fname' => 'required|max:255',
          'lname'  => 'required|max:255',
          'email'      => 'required|email|max:255|unique:users',
          'password'   => 'required|min:6|confirmed',
    );
        return Validator::make($data, $rules);
    }

getLoginCredentials

  protected function getLoginCredentials(Request $request)
  {
    $validator = User::loginValidation(Request::all());

    if($validator->passes())
    {
    return[
    'email'    => Request::input('email'),
    'password' => Request::input('password'),
    'type'     => 1  
    ];

    return true;
    }else{
        return redirect()->back()->withErrors();
    }
  }  
0 likes
15 replies
d3xt3r's avatar

Make it a static method and public for your purpose.

    public static function loginValidation($data)
    {
        $rules = array(
          'fname' => 'required|max:255',
          'lname'  => 'required|max:255',
          'email'      => 'required|email|max:255|unique:users',
          'password'   => 'required|min:6|confirmed',
        );
        return Validator::make($data, $rules);
    }
d3xt3r's avatar

Where have you defined the function ?

d3xt3r's avatar

What do i say ? Why do expect to access the User::method() when you have not defined it there,

User::method() ; // means you are accessing the method (static) defined in User class
$user->method() ; // means you are accessing the method defined in User class, from a user instance 

Define the method in User class and not custom Auth controller, or else if you wnat to define in Auth controller, access it like


protected function loginValidation($data)
    {
        $rules = array(
          'fname' => 'required|max:255',
          'lname'  => 'required|max:255',
          'email'      => 'required|email|max:255|unique:users',
          'password'   => 'required|min:6|confirmed',
    );
        return Validator::make($data, $rules);
    }

protected function getLoginCredentials(Request $request)
  {
    $validator = $this->loginValidation(Request::all());

    if($validator->passes())
    {
    return[
    'email'    => Request::input('email'),
    'password' => Request::input('password'),
    'type'     => 1  
    ];

    return true;
    }else{
        return redirect()->back()->withErrors();
    }
  }  

jzmwebdevelopment's avatar

I don't understand how I would bring the below methods into my functions

User::method() ; // means you are accessing the method (static) defined in User class
$user->method() ; // means you are accessing the method defined in User class, from a user instance 
sameermalikrp's avatar

Try this

 protected function loginValidation($data)
    {
        $rules = array(
          'fname' => 'required|max:255',
          'lname'  => 'required|max:255',
          'email'      => 'required|email|max:255|unique:users',
          'password'   => 'required|min:6|confirmed',
    );
$this->validate( $request , $rules);
    }
sameermalikrp's avatar

no need $data is enough, if that doesnt work means reply clearly , if you found good on this then please close this

jzmwebdevelopment's avatar

Thanks,

That gives me the error of Undefined variable: request but if I change $data to $request I get

Argument 1 passed to App\Http\Controllers\Controller::validate() must be an instance of Illuminate\Http\Request, array given, called in app/Http/Controllers/Admin/Auth/AuthController.php on line 72 and defined
sameermalikrp's avatar

so that $data you passed is an array so, just post where that data comes from and entire details about that

jzmwebdevelopment's avatar
Level 1

This is now resolved by doing the following:

 protected function loginValidation($request)
    {
        $rules = array(
          'fname' => 'required|max:255',
          'lname'  => 'required|max:255',
          'email'      => 'required|email|max:255|unique:users',
          'password'   => 'required|min:6|confirmed',
    );
        $this->validate( $request , $rules);
    }

 protected function getLoginCredentials(Request $request)
  {
    $validator = $this->loginValidation($request);

    var_dump($validator); die();

    if($validator->passes())
    {
    return[
    'email'    => Request::input('email'),
    'password' => Request::input('password'),
    'type'     => 1  
    ];

    }else{
        return redirect()->back()->withErrors();
    }
  }  

Please or to participate in this conversation.