Laravel eloquent model relationship

Prafulla Kumar Sahu

My app builds on

Laravel: 5.6.35
PHP 7.2.4
Entrust: 1.9

My Role model

class Role extends EntrustRole
{
    public function permissions()
    {
        return $this->belongsToMany(Permission::class);
    }

    public function users()
    {
        return $this->hasMany(User::class);
    }
}

And my User model is

class User extends Authenticatable
{
    public function role()
    {
        return $this->belongsTo(Role::class);
    } 
}

And now you can notice in Tinker

D:\work\www\myapp>php artisan tinker
Psy Shell v0.9.7 (PHP 7.2.4 — cli) by Justin Hileman
>>> App\models\Role::find(1)->users()->get()
Illuminate/Database/QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.role_id' in 'where clause' (SQL: select * from `users` where `users`.`role_id` = 1 and `users`.`role_id` is not null)'
>>> App\User::find(1)->role()->get();
=> Illuminate\Database\Eloquent\Collection {#2937
     all: [],
   }
>>> App\User::find(1)->roles()->get();
=> Illuminate\Database\Eloquent\Collection {#2941
     all: [
       App\models\Role {#2930
         id: 1,
         name: "super-admin",
         display_name: "Super Admin",
         description: "This will be one permission, that can not be assigned or modified.",
         created_at: "2018-09-07 12:11:35",
         updated_at: "2018-09-07 12:11:35",
         pivot: Illuminate\Database\Eloquent\Relations\Pivot {#2927
           user_id: 1,
           role_id: 1,
         },
       },
     ],
   }

I am getting result for App\User::find(1)->roles(), but my User model has function role(), and empty collection for App\User::find(1)->role() and error for App\models\Role::find(1)->users()

so please give some idea, how to solve this issue?

Prafulla Kumar Sahu

I think I found the answer to my question here.if you have properly defined relations through hasMany and belongsTo in your models, but haven't provided foreign key in the table of the model who belongsTo other table, your relations won't work. In documentation too, it suggests to use foreign_key to use One-to-Many relationship.

Entrust database design is based on many-to-many relationship. So that user can have multiple roles.Purly as described in Laravel documentation.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel Eloquent - accessing second level model relationship

From Dev

Laravel - eager loading a method (not a relationship) of an Eloquent model

From Dev

Model relationship with eloquent

From Dev

Laravel eloquent model how to get data from relationship's table

From Dev

Laravel/Eloquent - Order By a Model's Relationship Without Using Table Names

From Dev

How to set Eloquent relationship belongsTo THROUGH another model in Laravel?

From Dev

Laravel 5.2 Eloquent - Model through Many-To-Many Relationship

From Dev

Laravel Eloquent - firstOrCreate() on a relationship

From Dev

Laravel - Eloquent Self Relationship

From Dev

Laravel Eloquent Relationship Confusion

From Dev

Laravel Eloquent all with relationship

From Dev

Laravel Eloquent hasMany relationship

From Dev

Laravel eloquent Relationship Query

From Dev

Laravel Eloquent - firstOrCreate() on a relationship

From Dev

Laravel eloquent and relationship

From Dev

Named Relationship in Laravel Eloquent

From Dev

Laravel 4 Eloquent Relationship

From Dev

Laravel Eloquent hasMany relationship

From Dev

Eloquent Relationship with Laravel 5.4

From Dev

groupBy with eloquent relationship in laravel

From Dev

Laravel/Eloquent Relationship Inconsistent

From Dev

Eloquent ORM conditional model relationship

From Dev

Eloquent Model parent and child relationship

From Dev

Laravel, Eloquent n:m relationship

From Dev

Laravel Eloquent - sync() on a Morph relationship

From Dev

Laravel eloquent filter on hasMany relationship

From Dev

Laravel Eloquent Filter By Column of Relationship

From Dev

Laravel/Eloquent - Query - Select with relationship

From Dev

Get hasMany relationship with Eloquent Laravel

Related Related

HotTag

Archive