Auth :: attempt总是返回false,我也不知道为什么

joaopedro489
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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

不知道为什么我的 cursorForObjectInConnection 返回 null?

来自分类Dev

Laravel 5 Auth :: attempt总是返回false

来自分类Dev

Laravel 5 Auth :: attempt()总是返回false

来自分类Dev

Laravel 5 Auth :: attempt()总是返回false

来自分类Dev

为什么Laravel Auth :: check总是返回false?

来自分类Dev

我不知道为什么我的变量总是重置为0?

来自分类Dev

PHP SQL语句中的错误($ stmt2返回false,但我不知道为什么)

来自分类Dev

Laravel auth :: attempt返回true,但是auth :: check()false为什么呢?

来自分类Dev

我不知道为什么我得到AttributeError

来自分类Dev

我的陈述无效,我也不知道为什么

来自分类Dev

我不知道为什么我得到AttributeError

来自分类Dev

我的查询在wordpress中返回null,但我不知道为什么

来自分类Dev

我的查询在wordpress中返回null,但我不知道为什么

来自分类Dev

我的 Javascript 代码返回 null,我不知道为什么

来自分类Dev

Laravel Auth :: attempt()返回false

来自分类Dev

VLOOKUP给#N / A我不知道为什么

来自分类Dev

无限循环我不知道为什么(DichotomySearch)

来自分类Dev

PyGame不断崩溃,我不知道为什么

来自分类Dev

函数已执行,我不知道为什么

来自分类Dev

C ++ Segfault,我不知道为什么?

来自分类Dev

VLOOKUP给#N / A我不知道为什么

来自分类Dev

声音消失了,我不知道为什么

来自分类Dev

df挂起,我不知道为什么

来自分类Dev

不知道为什么我的python输出循环

来自分类Dev

标题缩进,我不知道为什么

来自分类Dev

不知道为什么我收到 StopIteration 错误

来自分类Dev

SMTP 错误,我不知道为什么

来自分类Dev

算法停止,我不知道为什么

来自分类Dev

我不知道为什么req.param('length')返回0

Related 相关文章

  1. 1

    不知道为什么我的 cursorForObjectInConnection 返回 null?

  2. 2

    Laravel 5 Auth :: attempt总是返回false

  3. 3

    Laravel 5 Auth :: attempt()总是返回false

  4. 4

    Laravel 5 Auth :: attempt()总是返回false

  5. 5

    为什么Laravel Auth :: check总是返回false?

  6. 6

    我不知道为什么我的变量总是重置为0?

  7. 7

    PHP SQL语句中的错误($ stmt2返回false,但我不知道为什么)

  8. 8

    Laravel auth :: attempt返回true,但是auth :: check()false为什么呢?

  9. 9

    我不知道为什么我得到AttributeError

  10. 10

    我的陈述无效,我也不知道为什么

  11. 11

    我不知道为什么我得到AttributeError

  12. 12

    我的查询在wordpress中返回null,但我不知道为什么

  13. 13

    我的查询在wordpress中返回null,但我不知道为什么

  14. 14

    我的 Javascript 代码返回 null,我不知道为什么

  15. 15

    Laravel Auth :: attempt()返回false

  16. 16

    VLOOKUP给#N / A我不知道为什么

  17. 17

    无限循环我不知道为什么(DichotomySearch)

  18. 18

    PyGame不断崩溃,我不知道为什么

  19. 19

    函数已执行,我不知道为什么

  20. 20

    C ++ Segfault,我不知道为什么?

  21. 21

    VLOOKUP给#N / A我不知道为什么

  22. 22

    声音消失了,我不知道为什么

  23. 23

    df挂起,我不知道为什么

  24. 24

    不知道为什么我的python输出循环

  25. 25

    标题缩进,我不知道为什么

  26. 26

    不知道为什么我收到 StopIteration 错误

  27. 27

    SMTP 错误,我不知道为什么

  28. 28

    算法停止,我不知道为什么

  29. 29

    我不知道为什么req.param('length')返回0

热门标签

归档