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 );
}
У меня есть эта функция для входа, я пробую с учителем модели, но всегда auth::attempt
дал мне ложь, но если я попробую с моделью пользователя, результат будет верным.
Мой Модель учителя
<?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
должна работать с учителем.