Get related table data in laravel - php

i have a rating table.
The model relationship:
Rating is belongs to user & User has many rating.
The scenario is users have a profile page. It will display all ratings and users information who rate for the particular user.
I using $ratings= Rating::where('user_id',$user->id)->get();
But this only return rating information which belong to the user. No rater information.
How can i get rater information by rater_id?

if you want to build relationship in your model then you can
public function rater() {
return $this->belongsTo(User::class, 'rater_id');
}
now you can get the rater
$ratings= Rating::with('rater')->where('user_id',$user->id)->get();

Related

want to view logged user company name, not id

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}}

Laravel WhereHas all data control

I have a customers table. These customers have a custumer_request table, and a customer can have more than one claim record. And there's a table of floors. The multiples table has a customer_request_floors table that is associated with the customer claims table. They are all related to each other. I want to filter customers who have demand according to the selected multiples when filtering customers.
In the search I use below, I want to filter it according to the latest requests of customers. When I filter as follows, it brings me linked records of that user's other requests. For example, let the customer have 2 requests. Let these be two different floor records. I'd like to search between multiples of the last request. When I filter as follows, it brings me the registered floor_id in 2 requests. I'm shooting your last request using a model. Can you help with filtering?
What I really want to look for is to filter the records with floor_id that I send in the pivot Table, which depends on the final demand from the customers.
Customer Model:
public function lastRequest()
{
return $this->hasOne(CustomerRequest::class)->latest();
}
Search Model:
$this->customers = $this->customers->whereHas('lastRequest', function ($customer_request){
$customer_request->whereHas('floors', function($floors) {
$floors->whereIn('floor_id', $this->params['floor_ids']);
});
});
CustomerRequest Model
public function floors()
{
return $this->belongsToMany(Floor::class, 'customer_request_floors');
}
customer_request_floors Pivot Table
protected $fillable = [
'customer_request_id', 'floor_id'
];

Laravel Eloquent accessing a 3rd relationship

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

Nested one to many relationships in Eloquent

Let's say I have three models with this relationsship
USER
hasMany (app/organisation)
ORGANISATION
hasMany (app/order)
ORDER
For one level I could just go
user->organisations()->get()
And get all the organisations tied to the user. But every organisation also have 0..many ORDER entities. So I want to do this:
user->organisations()->orders()->get()
To get a list of all the orders from all the organisations from a certain user. Is that possible this way?
What you can simply do is implement hasManyThrough() relationship on User model.
public function orders()
{
return $this->hasManyThrough(Order::class, organisation::class);
}
then you can query like,
$user->orders()->get();

Laravel relationships

Trying to figure what table I need to make this relationship to work...
I'd like the user to mark a post as favorite and save it so when he log back in, he can view all his favorite posts.
So I have User and Post models and their relationships of course.
User hasMany Post, Post belongTo User
Do I need to add Favorite model and add fav_id to both User and Post models ?
Or maybe adding pivot table?
If a favorite is defined as a simple relationship between users and posts, you don't need a model for it. However, you do want a pivot table, call it something like favorites. It would contain id, post_id, user_id, created_at, updated_at.
Then in your user model:
public function favoritePosts()
{
return $this->belongsToMany('Post', 'favorites');
}
And in the post model:
public function favoritedBy()
{
return $this->belongsToMany('User', 'favorites');
}
So now, for any given user, you can do $user->favoritePosts to get an array of posts that user has "favorited".
And from a post object, you can do $post->favoritedBy, to get an array of users that have favorited that post.
I haven't tested this but that seems like that would work off the top of my head.

Categories