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

Try this.

$users = User::with('services')
                   ->whereHas('services', function($q) {
                       $q->where('active', '=', 1); 
                   })
                   ->orderBy('created_at', 'desc')
                   ->get();
Last updated 2 years ago.
0

Thanks! Sadly that gives me the same error I was originally hitting,

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'active' in where clause is ambiguous

Last updated 2 years ago.
0

You'd have this problem writing raw SQL - you have to specify from which table the 'active' column is to be matched.

 $q->where('services.active', '=', 1); 
Last updated 2 years ago.
0

That nailed it! I thought I tried something like that earlier but I guess either not or I had something else wrong...

Many thanks

Last updated 2 years ago.
0
$q->wherePivot('active', 1);

is a more proper way to do it. wherePivot() and orWherePivot() methods are introduced with L4.1.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

lauriek lauriek Joined 26 Aug 2014

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.