我想使用不同的表而不是默认的用户表进行身份验证,而我使用学生表进行验证。
这是Student.php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Student extends Authenticatable
{
use Notifiable;
protected $table='students';
protected $fillable=['full_name','email', 'password'];
protected $hidden = [
'password', 'remember_token',
];
public function getAuthPassword()
{
return $this->password;
}
}
auth.php
'defaults' => [
'guard' => 'web',
'passwords' => 'students',
],
'guards' => [
'students' => [
'driver' => 'eloquent',
'model' => App\Student::class,
],
//this is last
'students' => [
'provider' => 'students',
'table' => 'password_resets',
'expire' => 60,
],
],
'providers' => [
'students' => [
'driver' => 'eloquent',
'model' => App\Student::class,
],
],
'passwords' => [
'students' => [
'provider' => 'students',
'table' => 'password_resets',
'expire' => 60,
],
],
];
auth / RegisterController
<?php
namespace App\Http\Controllers\Auth;
use App\Student;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
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, [
'full_name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:students'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
protected function create(array $data)
{
$api_token=str_random(60);
return Student::create([
'full_name' => $data['name'],
'email' => $data['email'],
'api_token'=>$api_token,
'password'=>bcrypt($data['password']),
]
);
}
}
我可以很好地进行注册,但是当我要登录时遇到此错误。
BadMethodCallException 调用未定义的方法App \ Student :: generateToken()
答案 0 :(得分:-1)
从 Laravel 5.7 l 起,助手'str_random'变为'Str :: rondom'。并且不要忘记能够使用“ 使用Illuminate \ Support \ Str; ”类