Search code examples
phplaravel-5registration

Getting User::create() not found in Laravel 5.5


When I try to register a user, I am getting an error:

Call to undefined method App\Models\User::create()

here is my User model:

<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User
{
use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'first_name', 'last_name', 'email', 'password',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];

protected $table = 'users';

}

I have used the User model that is shipped with Laravel 5.5, but just moved it to the Models folder. I updated the config/auth file to point to App\Models\User. I have ran php artisan optimize and composer dump-autoload several times, to no avail.

Here is my Register Controller:

<?php
namespace App\Http\Controllers\Auth;

use App\Models\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller {

public $titles = [];

public $title = 'Registration';

use RegistersUsers;

/**
 * Where to redirect users after registration.
 *
 * @var string
 */
protected $redirectTo = '/home';

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest');
}

/**
 * Get a validator for an incoming registration request.
 *
 * @param  array  $data
 * @return \Illuminate\Contracts\Validation\Validator
 */
protected function validator(array $data)
{
    return Validator::make($data, [
        'first_name' => 'required|string|max:255',
        'last_name' => 'required|string|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'password' => 'required|string|min:6|confirmed',
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return \App\User
 */
protected function create(array $data)
{
    return User::create([
        'first_name' => $data['first_name'],
        'last_name' => $data['last_name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
}

public function showRegistrationForm() {
    return view('auth.register')
      ->with('env', $this->env)
      ->with('titles', $this->titles)
      ->with('title', $this->title);
}
}

Solution

  • When extending Laravel User model from the Auth package, you should extend the User class from that package. The abstract class from package extends the Model and then you will have access to all model methods.

    From Illuminate\Foundation\Auth\User:

    class User extends Model implements
       AuthenticatableContract,
       AuthorizableContract,
       CanResetPasswordContract
    {
       use Authenticatable, Authorizable, CanResetPassword;
    }