anandshrestha57 anandshrestha57 - 4 years ago 172
PHP Question

i just make a register forum but validation doesnot work. I get following errors:BadMethodCallException in Builder.php line 2071:

Whenever I try to register form I get same error. Its been an hour but could not figure out. My validation does not working.

Even though I fully posted the register form it still shows same error.

What actually BadMethodCallException and that builder.php line 2071 that is out of my knowledge. I have following error:

Call to undefined method Illuminate\Database\Query\Builder::validate()


Here is my User@Controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use View;
use App\Basemodel;
use App\user;
use Input;

class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public $restful = true;

public function index()
{
return View::make('users.new')->with('title', 'Make it snappy Q&A - Register');
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$validation = User::validate(Input::all());

if($validation->passes()){
User::create(array(
'username'=>Input::get('username'),
'password'=>Hash::make(Input::get('password'))
));
return Redirect::to_route('home')->with('message','Thanks for registering!');
} else{
return Redirect::to_route('register')->with_errors($validation)->with_input();
}
}
}


and my Basemodel is:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use user;

class Basemodel extends Model
{
public static function validate($data){
return Validator::make($data,static::$rules);
}
}


and my user model is:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Basemodel;

class user extends Model
{
public static $rules = array(
'username'=>'required|unique:users|alpha_dash|min:4',
'password'=>'required|alpha_num|between:4,8|confirmed',
'password_confirmation'=>'required|alpha_num|between:4,8'
);
}

Answer Source

You are receiving that error because in your user model, you are extending the Model. You have defined your validate method in BaseModel. So to rectify that error, do the following:

Replace this line:

class user extends Model

With this line:

class user extends BaseModel

And the error should disappear.

Just a Side Note:

I don't know why you are doing in that way, but the correct way would be to create the FormRequest object.

Fire the following command from the terminal:

php artisan make:request YourFormRequest

The above command will create the file called YourFormRequest in app/Http/Requests folder. Open that file in your editor.

Now, in the rules method, pass all the validation rules. So, in your case it would be like this:

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'username'              => 'required|unique:users|alpha_dash|min:4',
        'password'              => 'required|alpha_num|between:4,8|confirmed',
        'password_confirmation' => 'required|alpha_num|between:4,8'
    ];
}

Now in your controller, update the create method to the following:

/**
 * Show the form for creating a new resource.
 * Import the YourFormRequest class
 *
 * @param \App\Http\Requests\YourFormRequest $request
 * @return \Illuminate\Http\Redirect
 */
public function create(YourFormRequest $request)
{
    User::create($request->all());

    return redirect('home')->with('message','Thanks for registering!');;
}

In order to hash the password, update the User model to this:

/**
 * Hash the password attibute before saving in database.
 * 
 * @param string $password
 */
public function setPasswordAttribute($password)
{
    $this->attributes['password'] = bcrypt($password);
}

With the setPasswordAttribute method, you don't have to worry about the hashing of password manually as laravel is smart enough to hash it with the above code.

Hope this helps you out. Cheers.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download