繁体   English   中英

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

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

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

$管理 - > adminRole->名称; 它会引发如下错误:

照亮/数据库/ QueryException与消息“SQLSTATE [42S22]:柱未找到:1054未知列'admin_roles.admin_id'在'where子句'(SQL:SELECT * FROM admin_roles其中admin_rolesadmin_id = 1和admin_rolesadmin_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');
    }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM