Please explain hasManyThrough relation - php

Is there any way to get all relational data with hasmanyThrough of third model that has many records with the relation with second model?
As example Main model : Exam id batch_id
relation- belongTo-Batch
second model : Batch id batch_name relation-hasMany-Exam and hasMany-Student
third mode :Student id name batch_id relation- belongTo-Batch
So using Exam model when I try to get batch that belong to it then I can retrieve it but How can I get all student through Exam model related to that batch? I have tried with hasManyThrough relation but not working...

If you were to use this in Exam:
public function students() {
return $this->hasManyThrough('Student', 'Batch');
}
This won't work, as hasManyThrough assumes that both Student and Batch belong to Exam (Exam belongs to batch)
The easiest way would be something like this: Exam::find(1)->batch->students

Related

Laravel multiple morph relation in one table

i have problem in laravel. i have created multiple morph in one table.
table structure
table_name : morphicable
id:
model_id: eg: '1'
model_type: eg 'model/comment'
model_one_id: eg: '1'
model_one_type: eg 'model/order'
in above relationship we can saving the data from comment to order and order to comment like many to manay.
Now the problem is that how to get the both data from one ORM method. like some time we save comment in model type and some time in model typeOne.
i want to get the data from both models in one go.
public function morphicable(): MorphMany
{
return $this->morphMany(morphicable::class, 'model');
}

Laravel: One to Many relationship, pull last N record from table

I have a User model and workLOG model. The user model has many work logs, meaning One user can have many work logs (ONE TO MANY RELATIONSHIP). I have created a relationship using the Eloquent relationship.
User Model
public function workLog()
{
return $this->hasMany('App\WorkLog','user_id','id');
}
workLog Model
class WorkLog extends Model
{
protected $table="worklog";
}
The worklog table has a column called total_hours_per_day. How can I pull the last N records from the column of the logged-in or authenticated user?
What I have done to get workLog so far
$worklogs = Auth::user()->workLog;
Now I want to get last 7 data from the column total_hours_per_day
Screenshots of the table
you need to establish on the WorkLog model a belongsTo.
public function user()
{
return $this->belongsTo('App\User');
}
Edit: relationships have to be established in both models you're trying to relate. If you like a girl, she needs to like you back to establish a relationship. You just defined the relationship in one side.
Try using take()
As Laravel documentation says:
"The take method returns a new collection with the specified number of items"
$worklogs = Auth::user()->workLog->take(-7)->reverse();
and I pull the last 7 records from total_hours_per_daycolumn in the blade template as:-
#foreach ($worklogs as $worklog)
{{ $worklog->total_hours_per_day }}
#endforeach

Laravel belongsToMany Relationship

Having a belongsToMany relation on the same model (Team Model) through custom pivot table (related_teams)
and my relation is like the following
now attaching and detaching is working just fine , however as you see every team belongs to a city
.
so my question is how can i get a list of distinct cities from those attached teams
It depends what you want to achive but for example
$this->attachedHotels->groupBy('city_id');
https://laravel.com/docs/6.x/collections#method-groupby
or
$this->attachedHotels->pluck('city_id')->unique();
https://laravel.com/docs/6.x/collections#method-pluck
https://laravel.com/docs/6.x/collections#method-unique

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: Complicated Eloquent Relationship - hasManyThrough or belongsToMany approach?

I have three Models (Organization, User, Visit) and 4 tables (organizations, users, organization_user, visits). I'm trying to get the accumulative total of user visits for an Organization.
Organization
------------
id,
name
User
---------
id,
name
Organization_User
----------------
id,
organization_id,
user_id
Visit
--------------
id,
user_id
views
Just to clarify, there is no Organization_User model, that is just the pivot table used by User and Organization:
$organization->belongsToMany('User');
$user->belongsToMany('Organization');
I could query all the user_ids from the pivot table by group_id, and then get all the visits for each user_id, but what's the more Eloquent approach?
A User hasMany Visits and a Visit belongsTo a User. A Visit doesn't belong to an Organization.
Solved it by using whereIn(). Basically with no change to my current relationship setup...to get accumulative views I did this:
$org = Organization::find($org_id);
return DB::table('visits')->whereIn('user_id', $org->users->modelKeys())->sum("views");
The modelKeys() returns all the user ids tied to that Organization. Then I get the sum of all the views for those users.
*Note it's also important to use Organization::find and not DB::table('organization') in order to maintain the Eloquent relationship. Otherwise $organization->users will give an error.
I think you might want a 'Has Many Through' relationship like so:
class Organization extends Model
{
public function visits()
{
return $this->hasManyThrough('App\Visit', 'App\User');
}
}
Then you could call count().
Laravel 5.1 doc

Categories