提问者:小点点

Auth::尝试总是返回false


我对拉威尔6号有意见。x身份验证。Auth::trunt()始终返回false。请帮帮我。谢谢大家!

我的User.php

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Comment;
use App\Models\Post;
use App\Models\Role;

class User extends Authenticatable
{
    use Notifiable;
    use SoftDeletes;
    protected $table = "users";
    protected $dates = ['deleted_at'];

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

    /**
     * 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 comments()
    {
        return $this->hasMany(Comment::class, 'user_id', 'id');
    }

    public function posts()
    {
        return $this->hasMany(Post::class, 'user_id', 'id');
    }

    public function role()
    {
        return $this->belongsTo(Role::class, 'role_id', 'id');
    }

}

我的用户控制器。php

public function loginAdmin(Request $request)
    {
        if (Auth::attempt(['email' => $request->email, 'password' => $request->password])) {
            return redirect('admin');
        } else {
            return redirect('admin/login')->with('notify', 'Đăng nhập không thành công !');
        }
    }

我的表单登录

<form class="login-form" action="admin/login" method="POST">
                @csrf
                <div class="form-group">
                    <div class="form-label-group">
                        <input type="email" id="inputEmail" name="email" class="form-control"
                               placeholder="Email address"
                               required="required" autofocus="autofocus">
                        <label for="inputEmail">Email address</label>
                    </div>
                </div>
                <div class="form-group">
                    <div class="form-label-group">
                        <input type="password" id="inputPassword" name="password" class="form-control"
                               placeholder="Password"
                               required="required">
                        <label for="inputPassword">Password</label>
                    </div>
                </div>
                <button type="submit" class="btn btn-primary btn-block">Login</button>
            </form>

我的函数是createUser。这是我用来创建用户的函数。我在保存数据后使用bcrypt,但它仍然不起作用。我不知道我的问题在哪里。请帮帮我。

function createUser($attributes)
    {
        $data = $attributes->all();
        $data['password'] = bcrypt($attributes->passowrd);
        if (!isset($data['admin'])) {
            $data['admin'] = 0;
        }
        if ($data['admin'] == 0) {
            $data['role_id'] = null;
        }
        return $this->create($data);
    }

这是我的问题的所有代码。请帮助我。谢谢大家!


共1个答案

匿名用户

有一个输入错误,请修复为“``$attributes”-