Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hakan's avatar
Level 1

How to get data from many to many relationship

From a many to many relationships, I have three tables, "companies", "activities" and "activity_company".

Basically, I want to run this query with Laravel eloquent but im facing some issues

QUERY: select *from companies c join activity_company ac on c.id=ac.company_id where ac.countrycode='ml' and c.businessclub_id is not null');

NOTE: countrycode is an attribute of "activity_company"

I need Help please

0 likes
7 replies
ftrillo's avatar

Assuming you setup the relationships in the Eloquent, try this:

I don't actually know if that would work for sure, but give it a try.

$companies = Company::whereNotNull('businessclub_id')->whereHas('activities', function($query) {
    // $query->wherePivot('countrycode', 'ml'); // Apparently this doesn't work
    $query->where('countrycode', 'ml'); // But this does
})->get();
1 like
hakan's avatar
Level 1

Thanks for answering back. It is not working, here is the error im getting SQLSTATE[42S22]: Column not found: 1054 Unknown column 'pivot' in 'where clause' (SQL: select * from companies where businessclub_id is not null and exists (select * from activities inner join activity_company on activities.id = activity_company.activity_id where companies.id = activity_company.company_id and pivot = countrycode))

hakan's avatar
Level 1

It is working now, with this code: $query->where('countrycode', 'ml'); instead of $query->wherePivot('countrycode', 'ml');

Thanks for your help, But what if I want to retrieve and display a data from both table EXAMPLE:

$BM=Company::whereNotNull('businessclub_id')->whereHas('activity', function($query) { $query->where('countrycode','=','ml'); })->get();

@foreach ($BM as $BMS)

<a class="label label-primary" href="#">{{$BMS->activity->name}}</a>
<h3><a href="#">{{$BMS->name}}</a></h3>
                                

@endforeach

I am getting this Error: "Property [name] does not exist on this collection instance. (View: C:\Users\KEITA\ibafrica\resources\views\homes\index.blade.php)" and it is related to this {{$BMS->activity->name}}

tisuchi's avatar

@hakan

Use ``` before starting and after ending your code to make your code readable.

5 likes
hakan's avatar
Level 1

OK TISUCHI, let me give a try

I want to retrieve and display a data from both table EXAMPLE:

$BM=Company::whereNotNull('businessclub_id')->whereHas('activity', function($query) { $query->where('countrycode','=','ml'); })->get();
@foreach ($BM as $BMS)
<a class="label label-primary" href="#">{{$BMS->activity->name}}</a>
<h3><a href="#">{{$BMS->name}}</a></h3>                             
@endforeach 

I am getting this Error: "Property [name] does not exist on this collection instance." and it is related to this {{$BMS->activity->name}}

ftrillo's avatar
ftrillo
Best Answer
Level 4

$company->activity->name wont work because a single company can have more than one activity. Knowing that:

$company->activity; // returns a collection of activity models
$company->activity->first(); // Gets you the first activity of the company

The relation should be called activities, instead of activity, to avoid being missleading.

hakan's avatar
Level 1

That worked! Thank you so much! Your explanation was perfect!

Please or to participate in this conversation.