Level 73
That's fine as long as you have the relationship setup correctly. Proving that it is correct Eloquent you can see it used in the docs too: https://laravel.com/docs/8.x/eloquent-relationships#querying-relationship-existence
1 like
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Newbie question here :-)
Is this the proper way to 'where' in belongstoMany relationship?
$platforms = Platform::with('users')
->whereHas('users', function ($q) {
$q->where('id', '=', Auth()->user()->id);
})
->orderBy('id', 'desc')
->get();
Looks like I'm not using proper Eloquent for this...
You code could look like this
$platforms = Platform::with('users')
->whereRelation('users', 'id', auth()->id())
->orderByDesc('id')
->get();
Please or to participate in this conversation.