简体   繁体   中英

Getting all users that created an item today in Laravel

I want to make a list of all users that created an item.

These items have the row user_id

To get all items created today, I do this:

$items = Item::whereDay('created_at', '=', date('d'));

Now I want to perform a query that gives me all users who have created an item today. There will be users who created 1000 items, and there will be users that didn't do anything at all.

I could do this with ->unique() but there must surely be a better way to do this.

Any suggestions?

Assuming you have the items relationship set on your User model you could do something like:

$users = User::whereHas('items', function ($query) {
   $query->where('created_at', '>=', \Carbon\Carbon::today());
})->get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM