I have generate the following database table and need to create model form them.
Creating a model seems to be an easy task, but I am confused how should i define the relationship between two tables. In users table RoleId is foreign key. My question is where and how should I define the relationship (in User model or Role model). Should I use hasOne, hasMany, or belongsTo
In your User model
public function roles()
{
return $this->hasOne('Role', 'id', 'RoleId');
}
In your Role model
public function users()
{
return $this->belongsTo('User', 'RoleId', 'id);
}
Related
I have three tables users, profiles and roles. The users and profiles tables has one to one relationships and the users and roles has many to many relationships using a pivto table role_user. In the Role model, I retrieve related users:
Role model
public function users()
{
return $this->belongsToMany(User::class);
}
My problem is that the above method only retrieves the users and not their profiles. In fact, I want to get users as well as their profiles using a role model and then paginate them.
public function getUsers($role)
{
return $role->users()->paginate(50);
}
So, how can I retrieve users and their profiles using a Role model?
In your User model you define your Profile relationship:
public function profile()
{
return $this->hasOne(Profile::class);
}
and then when you call the getUsers() method you can eagerload the users and their profile:
$role = Role::with('users.profile')->find($id);
$result = getUsers($role);
I've got a Company model in which I would like to define two relationships with the User model:
public function users(){
return $this->hasMany('App\User');
}
public function administrator(){
return $this->hasOne('App\User', 'superuserid');
}
I have a superuserid field in my companies table in the database, which is a foreign key to the id of the users table.
I just want to know if this is at all a good strategy or even possible?
Yes, it is a good strategy and it is possible.
In your model you can define as much relations as your model needs. And those relations can be of different types (hasOne, hasMany, belongsTo, etc).
In your case, since the company has multiple users and belongs to a super user, we have two different relations, so, obviously, we need to create two methods in the model to represent these two relations.
Another example: the relation between you and your father. Let's say that there is this implementation of a User model:
class User extends Model
{
public function children()
{
return $this->hasMany(User::class, 'id_parent');
}
public function dad()
{
return $this->belongsTo(User::class, 'id_parent')->where('gender', 'male');
}
}
Here a User can has many children but belongs to just one dad.
So I understand how to load the roles for a user and the permissions for a role.
But now I have a user table, a role table, and a permission table. I also have role_user table for linking users and roles. And of course a permission_role for linking permissions and roles.
Now when I want to get all the roles for the user, I simply do something like this:
public function roles()
{
return $this->belongsToMany('Uppdragshuset\AO\Tenant\Models\Role');
}
Similarly I can fetch permissions for roles like so:
public function permissions()
{
return $this->belongsToMany(Permission::class);
}
Now according to the documentation on laravel, I can directly fetch permissions for a user by using the hasManyThrough relation too like so:
public function permissions()
{
return $this->hasManyThrough(Permission::class, Role::class);
}
But this is returning with an error saying:
Unknown column 'role.user_id' in 'field list'
I think I understand why. Laravel is looking for user_id field in the role table but it does not understand that it is a many to many relation and it should look for it in the pivot table.
So what is the way around this? Is there a way around this in Eloquent or will I have to resort to using the query builder? And if yes, how to do the same thing with the query builder?
HasManyThrough can only be used to connect two HasMany relationships. There is no native relationship for your case.
I created a HasManyThrough relationship with unlimited levels and support for BelongsToMany:
Repository on GitHub
After the installation, you can use it like this:
class User extends Model {
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function permissions() {
return $this->hasManyDeep(Permission::class, ['role_user', Role::class, 'permission_role']);
}
}
Hello i am trying to get one user with his relationships. So one user form user table could have many items, so relationship is one to many.
Booth model
public function booths() {
return $this->hasMany('App\Booth');
}
User model
public function users() {
return $this->belongsTo('App\User');
}
I set foreign key from Booths table user_id is connected with Id from User table.
I try this query
$booth =User::findOrFail(83)->booths()->get();
But get all users. Also tried with with() but in that case i get all booths.
Change your code to
$user = User::with('booths')->findOrFail(83);
So, I've been trying to watch and read eloquent relationships from laracasts. Unfortunately I still don't quite get how to translate database relationships to eloquent relationships (hasOne, belongsTo, hasMany, etc).
Let's say I have an Account and Customer tables. Account table has a "Customer_id" foreign key which references to the "id" on Customer table. Let's assume it's a one-to-many relationship. How should I put it on my models on laravel?
Which table should contain the "hasMany" and which one should have the "belongsTo"?
Just think about how you would say it. In your case it sounds like a Customer has many Accounts and an Account belongs to one Customer.
So you would put the hasMany() in your Customer model and the belongsTo() in your Account model.
class Customer extends Model {
public function accounts() {
return $this->hasMany('App\Account');
}
}
class Account extends Model {
public function customer() {
return $this->belongsTo('App\Customer');
}
}
You can read more about Laravel database relationships here.