public function login( Request $request ) {
$fields = [
'email' => $request->email,
'password' => $request->password,
];
$access = Auth::attempt( $fields );
echo $access;
if ( $access ) {
$user = Auth::teacher();
$token = $user->createToken('MyApp')->accessToken;
return response()->json( [
"message" => "Login realizado com sucesso!",
"data" => [
'user' => $user,
'token' => $token
]
], 200 );
} else {
return response()->json( [
"message" => "Email ou senha inválidos!",
"data" => null,
"return" => $access,
], 401 );
}
我可以使用此功能登录,我尝试过使用Model Teacher,但始终auth::attempt
给我错误,但是如果尝试使用User Model,则结果为true。
我的模特老师
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;
use App\Student;
use App\Rating;
use App\Commentary;
use App\User;
class Teacher extends Authenticatable
{
use Notifiable;
use HasApiTokens;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function students(){
return $this->belongsToMany('App\Student')->withTimestamps();
}
public function ratings(){
return $this->hasMany('App\Rating');
}
public function commentaries(){
return $this->hasMany('App\Commentary');
}
public function updateTeacher(Request $req){
$validator = Validator::make($request->all(),[
]);
if($validator->fails()){
return response()->json($validator->errors());
}
if ($req->name)
$this->name = $req->name;
if ($req->email)
$this->email = $req->email;
if ($req->password)
$this->password = $req->password;
if ($req->number)
$this->number = $req->number;
if ($req->birth)
$this->birth = $req->birth;
if ($req->CPF)
$this->CPF = $req->CPF;
if ($req->lesson_price)
$this->lesson_price = $req->lesson_price;
if ($req->rent_price)
$this->rent_price = $req->rent_price;
if ($req->description)
$this->description = $req->description;
if ($req->district)
$this->district = $req->district;
if ($req->zone)
$this->zone = $req->zone;
if ($req->instruments)
$this->instruments = $req->instruments;
if ($req->certification)
$this->certification = $req->certification;
$this->save();
}
public function listTeachers(){
$paginator = Teacher::paginate(10);
return response()->json([$paginator]);
}
public function showTeacher($id){
$teacher = Teacher::findOrFail($id);
return response()->json([$teacher]);
}
}
我的模型使用者
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;
class User extends Authenticatable
{
use Notifiable;
use HasApiTokens;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
因此,我认为教师具有相同的功能和大多数操作(如用户),因此该功能auth::attempt
应与教师一起使用。
将教师模型添加到config / auth.php中,然后使用
Auth::guard('teacher')->attempt($credentials)
'guards' => [
'user' => [
'driver' => 'session',
'provider' => 'users',
],
'teacher' => [
'driver' => 'session',
'provider' => 'teachers',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'teachers' => [
'driver' => 'eloquent',
'model' => App\Teacher::class,
],
],
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句