BlueSuiter
BlueSuiter

Reputation: 567

No data fetched from associated table : Laravel

I have users and user_roles tables, and id of user_roles is used as foreign key in users. I fetched data from users using User::with('userRole')->find($user). In returned result userRole is present, however it is empty, instead it was supposed to have data from user_roles for the particular foreign key.

Please, share what can be the possible issues with the functionality or if anyone can explain working of laravel associations in brief.

/* User Model */
public function userRole()
{
    return $this->belongsTo('App\UserRole');
}

/* UserRole Model */
public function user()
{
    return $this->hasMany('App\User');
}

Thank You,

Upvotes: 1

Views: 212

Answers (2)

BlueSuiter
BlueSuiter

Reputation: 567

@Tim and @Nikola Thank you, for your efforts mates.

However, I found the reason behind the problem. It was because the wrong naming of userRole function in User model.

I was using foreign key of user_roles as user_roles_id in users table and defined function with name of userRole in User model. This leads to ORM not found the relevant column for attaching user_roles data.

The solution is either I have to change the name of user_roles_id to user_role_id or userRole function name to userRoles. And I choose the first one and it worked fine. :)

For reference on the naming conventions of laravel please refer to Laravel - Database, Table and Column Naming Conventions?.

Upvotes: 1

Nikola Gavric
Nikola Gavric

Reputation: 3543

Many to many relationships in Laravel often require you to have 2 models and 3 tables. One to many relationships require you to have 2 models and 2 tables. One to one relationship also requires you to have 2 models and 2 tables.

Many to many relationship

Let's take User and Role models, since each User can have multiple roles and one Role can be assigned to different users, you will naturally want a Many to many relationship. Now, you will need to create an intermediate table in which you will store the results, why? Because you already defined both User and Role and since it is Many to many none of those objects will have any identifier of the other one inside of them, but rather will have their own identifier in the intermediate table, and this is how Laravel fetches the relationship, it connects Models primary key with foreign key inside of the intermediate table.

One to many relationships

Let's take User and Role models again and let's say that this time, one Role can be assigned to multiple users, but one User can ONLY have 1 Role. Naturally you will have a field role_id inside of your User model and you will connect role_id from users with id from roles.

One to one relationships

Lets take User and Role models again :D Let's say you want to create a separate Role for every user, then you will have 2 models and 2 tables where your users table will have all the users and your roles table will contain id, name, user_id and now when you try to retrieve the relationship, if you define one to one laravel will return only 1 result, no matter if you have multiple same user_id on the roles table, Laravel will return only 1 role because you told him explicitly it's 1-to-1 relationship.

EDIT:

Here is an example of one to one relationship:

/* User Model */
public function userRole()
{
    return $this->belongsTo('App\UserRole', 'user_role_id', 'id');
}

Upvotes: 3

Related Questions