Support the ongoing development of Laravel.io →
Database Eloquent Queues
Last updated 2 years ago.
0

You need the "belongsToMany" relationship.

public function companies() {
    return $this->belongsToMany('ExCompany', 'companies_groups', 'company_id', 'group_id');
}

I'm not sure from the top of my head if the order of the keys (company_id and group_id) is correct, maybe you will need to switch them.

0

HI ftiersch.

I had a problem with connecting to the wrong instance of a database so I was not getting the results I was expecting so the many to many set-up didn't appear to be working.

When I start by obtaining a single group first and then getting the companies, I end up with a unique set of companies. I have a problem when I try and select multiple groups first and then get the companies afterwards I end up with a collection of groups and doing groups->companies does not seem to work.

How can I obtain a unique set of companies in one collection by any number of groups they belong in?

Thanks

Edit : I found what i needed here.

http://stackoverflow.com/questions/18403135/eloquent-orm-eager-loading-distinct-records-in-many-to-many-relationship

Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

sonesay sonesay Joined 1 Dec 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.