Try this
$today = Carbon::today();
$members = Member::whereHas('store_lists', function ($q) use ($today) {
$q->whereHas('location_list', function ($q) use ($today) {
$q->whereDate('date', '=', $today);
});
})->get();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm confuse on how to structure my tables or how to code a correct eloquent to get data I want, please enlighten me. I'm rookie.
I have tables
LocationList :
public function store_lists()
{
return $this->hasMany(StoreList::class, 'location_list_id');
}
StoreList :
public function location_list()
{
return $this->belongsTo(LocationList::class);
}
public function members()
{
return $this->belongsTo(Member::class); //??? or hasOne() ???
}
While location_lists store many store_lists data let's say 1 location might have 50 store_lists records
What I want to achieve is to get all members who listed in a store_lists table with a specific date
Will merging location_lists and store_lists better? If it is will it take more query time to get what I want?
And for the code I'm trying to achieve which is all wrong but i hope u get the idea.
Member::where( function ($q) {
$_date = substr(\Carbon\Carbon::today(), 0, 10);
$today = LocationList::where('date', $_date)->get();
// StoreList somewhere???
foreach ($today as $t) {
$q->where('location_list_id', $t->id); //<--- wrong again ...
}
})
What is the correct eloquent I should use please. Thank you.
Try this
$today = Carbon::today();
$members = Member::whereHas('store_lists', function ($q) use ($today) {
$q->whereHas('location_list', function ($q) use ($today) {
$q->whereDate('date', '=', $today);
});
})->get();
Please or to participate in this conversation.