Then you need to make a many to many relationship between Services and Appointments with the related pivot table and I would make another hasManyThrough as follow:
class Client extends \Eloquent {
public function appointments()
{
return $this->hasManyThrough('Appointment', 'Service');
}
}
The appointment_service pivot table should be used by the many-many relationship between appointments and services, and the way to access to appointments or services would be each of both tables using the hasManyThrough, I think.
Could you put the code that you are using within your models here?
Hmm, very confusing to me...
This is what i got so far:
class Client extends Model {
public function appointments()
{
return $this->hasMany('Appointment');
}
public function treatments()
{
return $this->hasManyThrough('Service','Appointment');
}
}
class Appointment extends Model {
public function client()
{
return $this->belongsTo('Client');
}
public function services()
{
return $this->belongsToMany('Service');
}
}
class Service extends Model {
public function appointment()
{
return $this->belongsToMany('Appointment');
}
}
treatments() in my Client should return all treatments (grouped) through his appointments.
For my project I had user, clubs, user_clubs, and user_club_badges. I originally created user_clubs as a pivot and I totally regret it. There's a way to do a triple pivot but I don't totally trust it and I have lots of functionality attached to this whole process so ultimately I'm thinking the best approach is to make a model called UserClub (or Memberships).
Using this approach it's trickier to do $club->users (you can I think but it's harder). Instead focus on doing $club->memberships (then foreach membership do $membership->user->id).
I've been waiting for a good solution for this, while there may be another one I wanted to share mine incase anyone else had the same frustrations. Save yourself the headache and don't use pivots for anything complex!!
@DivDax You may want something like this (I have seen it somewhere before, just can't recall where):
Client::with(['appointments.services' => function($query) use (&$services) {
$services = $query->get()->unique();
}]);
// $services will now be all the services for the client.
return $services;