1

I am building a multi authentication system using laravel 5.5. I have Admin and AdminRole Models as well as they respective migrations.There is one to one relationship between the Admin and AdminRole Models. Everything works fine. But when i tried to access the admin_role like so:

$admin->adminRole->name; It throws an error like so:

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)'.

I have tried for many hours but could not be able to figure out what the problem is. Any help will be greatly appreciated. Thanks in advance.

Admin.php Model:

<?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 migrations

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 Model

<?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 migrations

<?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');
    }
}
4
  • Quite strange. Your relationship definitions are right. Does anything change if you explicitly specify the foreign key? return $this->belongsTo('App\Models\AdminRole', 'admin_role_id'); Commented Jan 29, 2019 at 12:03
  • I have also tried to specify the foreign key; but the problem remain the same Commented Jan 29, 2019 at 12:09
  • I have replicated your setup here. It's working. Can we see your controller/blade where you are doing the relationship call? Commented Jan 29, 2019 at 12:13
  • I am testing on tinker Commented Jan 29, 2019 at 12:20

1 Answer 1

1

The code was actually correct, i cleared my cache and reboot my system. It is working now. Thank you guys.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.