I have 4 models, User, Profile, Audit and AuditApplications.
My table structure looks something like:
User
- id
- ... other user-related fields
Profile
- id
- user_id
- ... various profile fields
Audit
- id
- user_id
AuditApplications
- id
- audit_id
- user_id
My User model looks like this:
public function profile()
{
return $this->hasOne('App\Profile');
}
public function auditApplications()
{
return $this->hasMany('App\AuditApplications');
}
So I can get the user and their related profile easily and also a list of audits a user has applied for.
Next, I have AuditApplications table which holds a list of user IDs and audit ids of users that have applied to an audit. My AuditApplications table has the following relationship.
public function user()
{
return $this->belongsTo('App\User');
}
So I can return a list of users that have applied to an audit like so:
$applicants = AuditApplications::where('audit_id', $audit->id)->with('user')->get();
All this works well as expected but now I want to access the user's profile when I get a list of applications. The relationship is a distance one so I want to get a list of users and their profiles in the AuditApplications model.
Is there a way of doing this in Eloquent without creating a loop in my controller to then loop over each of the users getting their profiles individually?
you just need to do ->with('user.profile')
Related
I have two related Tables $table->foreignId('company_id')->references('id')->on('companies');
users Table
companies Table
id
id
company_id
name
Second
in view I want to show logged user company name
{{Auth::user()->company_id->name}}
pls help : (
Assuming that many users can belong to a company, you'll want to define a One to Many relationship between your User and Company models.
On your User model, add the following method.
public function company()
{
return $this->belongsTo(Company::class);
}
Retrieving the name of the company for the currently logged-in user is done like so.
$name = Auth::user()->company->name;
You can use this code
{{Company::find(Auth::user()->company_id)->name}}
where Company is your company's model or you can define a one-to-one relationship in your User model like so
/**
* Get the company associated with the user.
*/
public function company()
{
return $this->belongsTo(Company::class);
}
And use it like this
{{Auth::user()->company->name}}
I'm working on a small project using Laravel, and I would like to create a like and dislike system, so a user can like one to many more users.
I already have a table called Users (default by Laravel), now I have created another table called favorite_user with id, user_id and liked_the_user.
How can I get all the users that a user already liked? Is my table correct or should I change something?
All what I want to do is add like and show the people I liked. I don't know how to make this relationship since I am new to Laravel.
Thank you
Looks like you have a pivot table there so a Many to Many relationship is what you need to setup. You will need to pass some additional arguments since the naming won't fit with convention.
User Model:
// users the user has liked
public function liked()
{
return $this->belongsToMany(self::class, 'favorite_user', 'user_id', 'liked_the_user');
}
// users who like the user
public function likes()
{
return $this->belongsToMany(self::class, 'favorite_user', 'liked_the_user', 'user_id');
}
Controller:
public function liked(Request $request)
{
// get liked users for current user
$liked = $request->user()->liked;
// get liked users for an arbitrary user
$liked = User::findOrFail(...)->liked;
...
}
Laravel 8.x Docs - Eloquent - Relationships - Many to Many - belongsToMany
I have an understanding issue with Laravel Eloquent Models and relations.
I have a user table user. These users can add car license plates, which I save in user_plates. I have a database which contains car models and types. The table name is cars. I have the models App\User, App\UserPlates and App\Car.
The user_plates table has the fields id,plate,user_id,car_id.
I save the plate, the associated user (in user_id) and the selected car (car_id (id from table cars))
I added plates() with belongTo function to my User Model which already successfully returns all plates associated with that user. But now I want to get the associated car (car_id inside user_plates). How do I achieve this using Eloquent? The car table does not have any connection to the user table, only user_plates has a car_id and a user_id.
I need to achieve this:
User -> Plates (can be multiple) -> Plate -> Car. I know how to achieve this using simple MySQL Joins but I want to do it right with Eloquent. Thanks for any help!
Laravel: 6.4.0
So, if your database is set up as...
users user_plates cars
----- ----------- ----
id id id
etc. plate etc.
user_id
car_id
Your models are set up as...
// in model: User
public function user_plates()
return $this->hasMany('UserPlate'); // fill out fully qualified name as appropriateā¦
// in model: UserPlate
public function user()
return $this->belongsTo('User');
public function car()
return $this->belongsTo('Car');
// in model: Car
public function user_plates()
return $this->hasMany('UserPlateā);
To return a collection of cars belonging to user $id you should be able to run:
$cars = User::findOrFail($id)-> user_plates->pluck('car');
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);
I am creating an app where users have different roles. Some of the roles include:
Administrator
Supervisor
Contractor
I have the following tables setup:
user
role
role_user
The role_user is a pivot table used to store the user_id and role_id.
Now, when a Contractor is created, I trigger an event which assigns the user to the Contractor role in the database:
$user->roles()->save($role, ['cust_id' => $event->user->cust_id]);
The above line basically just creates a new row in the role_user pivot table and points to the role_id of a Contractor. This results in the user having Contractor privileges.
Also, within the event I need to assign the user to a Supervisor. However, there is no Supervisor model. Supervisors are also stored in the user table and have a role of Supervisor assigned to them.
How can I create a relationship between Contractors and Supervisors?
Ideally, i'd like to use something like this to create the Contractor/Supervisor relationship in the pivot table:
$user->supervisors()->save($supervisors, ['cust_id' => $event->user->cust_id]);
But i'm not sure how to setup my model...?
/**
* The supervisors that belong to the user.
*
* #return Object
*/
public function supervisors()
{
return $this->belongsToMany('SimplyTimesheets\Models\User\User')->withTimestamps();
}
What your asking is possible, however your relationship model would look like this:
public function cands()
{
return $this->belongsToMany('Some\Model')->where('something', $something)->where('something', $something);
}
You would have to use where clauses to hard code a relationship between contractors and supervisors because there is no direct relationship between them both.
/* Edit */
From your comment, you would have to approach that query with eager loading your relationship like so:
User::has('supervisors')->join(...)->where(...)->get();
The query above would return all users which meet the roles criteria. If you was just after one result you could create a hasOne relationship with your where clauses.
public function relationship()
{
return $this->hasOne('Your\User_Role\Model', 'foreign_key', 'other_key')->where('role_id', supervisor)->where('role_id', contractor)
}
It all comes down to your application needs.