我正在尝试以管理员身份登录。我使用了多个身份验证,我的用户工作正常,第二个用于管理,我正在尝试登录,但是当我在数据库中插入正确的值时,我手动插入了使用播种的值会给我一个错误,我不知道原因。
我所做的是配置 auth.php 所以我可以为用户和管理员进行两次身份验证。
<?php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'admins' => [
'driver' => 'session',
'provider' => 'admins'
]
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
// For admin
'admins' => [
'driver' => 'eloquent',
'model' => App\Models\Admin::class
]
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
'passwords' => [
'users' => [
'provider' => 'users',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
'admins' => [
'provider' => 'admins',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
],
];
create_admins_table - 迁移
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAdminsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('admins', function (Blueprint $table)
{
$table->increments('id');
$table->string('username');
$table->string('password');
$table->timestamps();
});
}
}
我的迁移文件已设置完毕。所以我迁移了它。
管理员 - 型号
<?php
namespace App\Models;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
class Admin extends Model implements AuthenticatableContract
{
use Authenticatable;
protected $guard = "admins";
protected $table = 'admins';
protected $fillable = [
'username',
'password',
];
protected $hidden = [
'password',
'remember_token'
];
}
AdminTableSeeder - 种子
<?php
use Illuminate\Database\Seeder;
class AdminTableSeeder extends Seeder
{
public function run()
{
DB::table('admins')->delete();
$admins = array(
array('username' => 'admin',
'password' => Hash::make('admin'))
);
DB::table('admins')->insert($admins);
}
}
AdminController - 控制器
<?php
namespace App\Http\Controllers;
use Auth;
use App\Models\Admin;
use Illuminate\Http\Request;
class AdminController extends Controller
{
public function postAdminLogin(Request $request)
{
$this->validate($request,
[
'username' => 'required',
'password' => 'required|min:7',
]);
if(!Auth::attempt($request->only(['username','password']), $request->has('remember')))
{
dd('error');
}
dd('Succesful!');
}
}
更新!!
所以我将用户名和密码更改为letmein
以避免混淆。我尝试登录正确的值,但我仍然无法登录。 !Auth:attempt
与我的数据库中的值不匹配,并打印出“错误”消息。
public function postAdminLogin(Request $request)
{
$this->validate($request,
[
'username' => 'required',
'password' => 'required|min:7',
]);
if(!Auth::attempt($request->only(['username','password']), $request->has('remember')))
{
dd('error');
}
dd('Succesful!');
}
admin.blade.php
<form class = "form-vertical" role = "form" method = "post" action = "{{ route ('auth.admin') }}">
<div class = "form-group {{ $errors->has('username') ? 'has-errors' : '' }}">
<label for = "username" class = "control-label">Username</label>
<input type = "text" name = "username" class = "form-control">
@if ($errors->has('username'))
<span class = "help-block">{{ $errors->first('username') }}</span>
@endif
</div>
<div class = "form-group {{ $errors->has('password') ? 'has-errors' : '' }}">
<label for = "password" class = "control-label">Password</label>
<input type = "password" name = "password" class = "form-control">
@if ($errors->has('password'))
<span class = "help-block">{{ $errors->first('password') }}</span>
@endif
</div>
<div class = "checkbox">
<label>
<input type = "checkbox" name = "remember">Remember me</input>
</label>
</div>
<div class = "form-group">
<button type = "submit" class = "btn btn-default">Login</button>
</div>
<input type = "hidden" name = "_token" value = "{{ Session::token() }}">
</form>
答案 0 :(得分:0)
你的表名在这里是问题...我们认为它应该是AdminTableSeeder中的“admins”,如下所示:
use Illuminate\Database\Seeder;
class AdminTableSeeder extends Seeder {
public function run() {
DB::table('admins')->delete(); //<== "admins" AS DEFINED IN YOUR CONFIG::: NOT "admin"
$admin = array(
array('username' => 'admin',
'password' => Hash::make('admin'))
);
DB::table('admins')->insert($admin);
}
}
答案 1 :(得分:0)
AdminTableSeeder - 种子
使用truncate而不是delete
use Illuminate\Database\Seeder;
class AdminTableSeeder extends Seeder
{
public function run()
{
DB::table('admins')->truncate();
$admins = array(
array('username' => 'admin',
'password' => Hash::make('admin'))
);
DB::table('admins')->insert($admins);
}
}
答案 2 :(得分:-2)
错误消息旁边的SQL查询显示
select count(*) as aggregate from `admin` where `password`=admin
现在,在您的WHERE子句中,密码被重新添加,因此我假设它是一个列(不是表)。反引号通常在SQL中用于表名或列名。但是你将列名称与admin进行比较,没有刻度。 &#39;管理&#39;不是保留字,并且在查询中不会引用它作为文本字符串,因此数据库服务器开始查找具有该名称的表以匹配。
那么,你试过在那里添加引号吗?
SELECT COUNT(*) AS aggregate FROM `admin` WHERE `password`='admin'
编辑:
如果所有其他方法都失败了,您可能还想简单地转储正在发布的数据库查询。添加一个监听器/转储:
\DB::listen(function($sql, $bindings, $time) {
var_dump($sql);
var_dump($bindings);
var_dump($time);
});
..并检查输出。