Reputation: 931
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
whereadmin_roles
.admin_id
= 1 andadmin_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');
}
}
Upvotes: 1
Views: 672
Reputation: 931
The code was actually correct, i cleared my cache and reboot my system. It is working now. Thank you guys.
Upvotes: 1