Try this.
$users = User::with('services')
->whereHas('services', function($q) {
$q->where('active', '=', 1);
})
->orderBy('created_at', 'desc')
->get();
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
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);
That nailed it! I thought I tried something like that earlier but I guess either not or I had something else wrong...
Many thanks
$q->wherePivot('active', 1);
is a more proper way to do it. wherePivot() and orWherePivot() methods are introduced with L4.1.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community