J
J
Jedi2020-05-18 02:24:22
Laravel
Jedi, 2020-05-18 02:24:22

Is it possible to connect a second model in addition to the "relationships"?

return $this->hasManyThrough(
            'App\Owner',
            'App\Car',
            'mechanic_id', // Foreign key on cars table...
            'car_id', // Foreign key on owners table...
            'id', // Local key on mechanics table...
            'id' // Local key on cars table...
        );


How to get also Car ?
How can I do that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Q
qvisn00, 2020-05-18
@PHPjedi

If intermediate models are also needed, then it is better to use not hasManyThrough as in your example, but hasMany/belongsTo.
Those. of the original model you prescribe the connection

public function owner(){
   return $this->belongsTo('App\Owner');
}

In the Owner model, you prescribe a connection
public function cars(){
  return $this->hasMany('App\Car');
}

And voila - you get everything you need right away through with:
Model::with('owner.cars')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question