1 month ago

Eloquent relationships that deal with 3 tables

Posted 1 month ago by scottsuhy

I have made all the one to many relationships in 3 models ‘user’, ‘auctionschedule’ and ‘auctionregistrations’ for “hasMany” and “belongsTo”. Here is what they look like

This allows for some very powerful (easy to create) queries. For example, this query is great:

It gives me a count of all the auctions the user has scheduled that are after Now()

$count = auth()->user()->auctionschedule()->where('date', '>=', Now())->count();

But how do I get a count of all the auctions the user is registered for that are scheduled for after Now(). Note that the auction date is in the auctionschedule table. So with this query, all 3 tables are involved. I’ve tried everything I can think of such as this but nothing seems to work-example:

$count = auth()->user()->auctionregistrations()->where('', '>', Now())->count();

Please sign in or create an account to participate in this conversation.