我正在使用laravel 5.5构建多身份验证系统。我有Admin和AdminRole模型以及它们各自的迁移。Admin和AdminRole模型之间存在一对一的关系。一切正常。但是,当我尝试像这样访问admin_role时:
$ admin-> adminRole->名称;它会引发如下错误:
带有消息“ SQLSTATE [42S22]”的Illuminate / Database / QueryException:找不到列:1054“ where子句”中的未知列“ admin_roles.admin_id”(SQL:从
admin_roles
中选择*,其中admin_roles
。admin_id
= 1和admin_roles
。admin_id
不能为空限制1)'。
我已经尝试了好几个小时,但无法弄清楚问题出在哪里。任何帮助将不胜感激。预先感谢。
Admin.php模型:
<?php
namespace App\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Admin extends Authenticatable
{
use Notifiable;
protected $guard = 'admin';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'ip_address',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function adminRole() {
return $this->belongsTo('App\Models\AdminRole');
}
}
admins.php迁移
use Illuminate\Support\Facades\Schema;
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->integer('admin_role_id')->unsigned()->nullable();
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->ipAddress('ip_address')->nullable();
$table->string('photo')->default('avatar.png');
$table->boolean('status')->default(true);
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('admins');
}
}
AdminRole.php模型
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class AdminRole extends Model
{
//
protected $fillable = ['name'];
public function admin()
{
return $this->hasOne('App\Models\Admin');
}
}
admin_role.php迁移
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAdminRolesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('admin_roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('admin_roles');
}
}
答案 0 :(得分:0)
该代码实际上是正确的,我清除了缓存并重新启动了系统。现在正在工作。谢谢你们。