Illuminate/Database/QueryException 消息 'SQLSTATE[42S22]:未找到列:1054 未知列

Illuminate/Database/QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column

我正在使用 laravel 5.5 构建多重身份验证系统。我有 Admin 和 AdminRole 模型,它们各自 migrations.There 是 Admin 和 AdminRole 模型之间的一对一关系。一切正常。但是当我尝试像这样访问 admin_role 时:

$admin->adminRole->name;它会抛出这样的错误:

Illuminate/Database/QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'admin_roles.admin_id' in 'where clause' (SQL: select * from admin_roles where admin_roles.admin_id = 1 and admin_roles.admin_id is not null limit 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');
    }
}

代码实际上是正确的,我清除了缓存并重新启动了系统。它现在正在工作。谢谢大家。