Modelling the relationship between User and Rank models - php

I am using Laravel & Eloquent.
I have two tables.
User
id, username, password, rankId
Rank
id, rankName
What would be the relationship between both of these? And, how would I possible show the relationship between both of these models using Laravel Eloquent Models? Any help would be highly appreciated.
I thought of hasOne, but then, it requires the second table (rank) to have a foreign key to the users table. How would I go about defining this relationship in Laravel?

If you are on User model,that will belongs to relation,like
public function rank()
{
return return $this->belongsTo('App\Rank', 'rankId', 'id');
}
Now if you query from User model,you can get rank information like this,
$user = User::find(1);
var_dump($user->rank->rankName);
Now if you are on Rank model and you want to get all user in a specific rank,so your relation will be has many,like this
public function users()
{
return $this->hasMany('App\User', 'rankId', 'id');
}
Now if you query a rank,you can get user by this way
$ranks = Rank::find(1);
foreach($ranks->users as $user){
var_dump($user->username);
}
May be everything is clear to you now

Related

How to set value foreign key of foreign key Laravel

Visit Model
id
user_id
visit_date
public function user()
{
return $this->belongsTo(USER::class,'user_id','user_id')->withTrashed();
}
User Model
user_id
branch_id
name
public function branch()
{
return $this->belongsTo(BRANCH::class,'branch_id','branch_id')->withTrashed();
}
Branch Model
branch_id
branch_desc
I want to display branch_desc when getting list of Visit.
Currently Im showing only branch_id from User Model
You need to make one to Many relation from visit to user table and user to branch table
ex. of relationship code:
//From Visit to User model
$this->belongsTo(User::class, 'user_id')
//From User to Branch Model
$this-belongsTo(Branch::clas, 'branch_id')
This will allow you to eager load the relationships in you controller like this
Visit::with(['user.branch'])
//Now you will get all visits with neseted users and branch
I will recommend you to take a look at laravel relationships docs for a better understanding
Laravel Relationships with eager loading
For 2 layers (or more) relationships, I recommend you install this package from Staudenmeir https://github.com/staudenmeir/eloquent-has-many-deep
Inside your Visit model, you can add the relationship like this:
public function branches() {
$query-> hasManyThrough(Branch::class, User::class);
}
And for more information , check LaravelDaily tutorial related to this package https://youtu.be/wgdWokrm3Mw
Your question isn't clear really! However from what I understand from your question you should also have a Branch model and migration.
So, from visit table you will fetch the users by the relation with user in your visit model.
public function user()
{
return $this->belongsTo(User::class, 'id', 'user_id');
}
Then when you can fetch your user branches from your User model by your users relation with Branch.
public function branch()
{
return $this->hasMany(Branch::class, 'user_id', 'id');
}
So, you will fetch data like this:
$visit->user->branch->description
You can replace description with any column_name_that_wish_to_fetch.
If figured it out.
Use this in getting list of your model in Controller.
This is how to query with multiple relationship
$visit= VISIT::with('USER.BRANCH')->orderBy('user_id','desc');

Laravel Many to Many Relationship ( hasMany or belongsToMany )

I am currently building my first App with Laravel and have stumbled upon the problem that I dont know how to setup the relationship Many-to-Many between the Models (User and Group).
I've created a board in which I store the relationship between all users and the Group they are in.
My Problem is that I dont know how to acces and set this up in Laravel.
Im not sure whether I have to user hasMany or belongsToMany.
I am trying to find a method to add a User to Group, so that a new entry will be created in the UserGroups table.
My tables:
User
ID
Name
Email
Group
ID
Name
Creator_ID
UserGroup
User_ID
Group_ID
I appreciate any help, thanks!
If you want to create a many-to-many relationship, it should be belongsToMany, not hasMany.
In the Group model:
public function users()
{
return $this->belongsToMany(Group::class);
}
And in the User model:
public function groups()
{
return $this->belongsToMany(User::class);
}
The pivot table should be called group_user.
https://laravel.com/docs/5.4/eloquent-relationships#many-to-many
In the model class, use belongsToMany as demonstrated here: https://laravel.com/docs/5.4/eloquent-relationships#many-to-many
To add a user to a group, use attach(), as demonstrated here:
https://laravel.com/docs/5.4/eloquent-relationships#the-create-method (scroll to many to many relations)

Laravel eloquent many to many relationship determine if relation exist

I am working on a project with many-to-many relationship between two tables roles and users. I also have a pivot table role_user that holds information about the relation between the other two tables.
role_user two fields namely the ids of the two tables: role_id and user_id.
Now I have a user object gotten from the users table and another role object gotten from the roles table. I want to determine if a particular role belongs to a certain user.
I know this can be done by creating a model for the pivot table and then using the model, the role_user table can be queried and determined if the relation exists.
But I find this method stressful and wonder if eloquent provides an easy method to determine this.
Thanks for any help?
You don't need to have a role_user model. You might already have role model and the user model related to the tables you have.
Specify the many to many relationship in Role.php and User.php
User.php - function roles
public function roles()
{
return $this->belongsToMany('App\Role');
}
Role.php - function users
public function users()
{
return $this->belongsToMany('App\User');
}
You may have to define other parameters like pivot table etc if you don't follow the naming conventions in table columns and tables. Since you seem to have follow the rules I'm not going to explain it. But you always can refer to laravel documentation's eloquent many-to-may section
now you can easily check whether a user belongs to a certain role.
Consider the following examle
//select a user
$user = App\User::find(1);
//select a role
$role = App\Role::find(1);
//get $user's roles. This ill return an array of role objects that are belong to $user
$userRoles = $user->roles;
//check whether $role is in that array
if(in_array($role, $userRoles)){
//do something
}

Laravel eloquent get specific user with his relations

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);

Laravel 5 - return only one field from relationship

have a relationship which returns rows from a pivot table.
However I just want to return a collection of the user ids from the table.
If I have it as current:
public function followers()
{
return $this->hasMany('App\Follower')->select(array('project_id', 'user_id'));
}
It will return the 2 fields, but if I just have the select with a single field:
public function followers()
{
return $this->hasMany('App\Follower')->select( 'user_id');
}
It returns an an empty object.
Any ideas?
I'm guessing this function is in your Project model. When modifying the select on a relationship, you must, at a minimum, select the fields needed to match the records together.
In this case, if this in on your Project model, if you don't select the project_id from the related table, Laravel can't match up the Follower objects with the correct Project objects, since Laravel won't have any idea which Project the Follower belongs to without the foreign key.

Categories