Many to many deep #214
-
Hey, I have the following structure:
I need to have a Here is how I do it with plain SQL: -- get all sport_parks by customer
SELECT sport_parks.* FROM sport_parks
INNER JOIN fields ON fields.sport_park_id = sport_parks.id
INNER JOIN field_customer ON field_customer.field_id = fields.id
WHERE field_customer.customer_id = 1
GROUP BY sport_parks.id |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hi @dpetrovaliev, class Customer extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function sportParks()
{
return $this->hasManyDeep(SportPark::class, ['field_customer', Field::class], [null, null, 'id'], [null, null, 'sport_park_id']);
}
} Try adding $uniqueSportParks = $customer->sportParks()->distinct()->get(); |
Beta Was this translation helpful? Give feedback.
-
@staudenmeir
|
Beta Was this translation helpful? Give feedback.
Hi @dpetrovaliev,
Use this relationship:
Try adding
->distinct()
to get unique results: