Finding a user with highest post created in last 24 hours, laravel Eloquent
up vote
2
down vote
favorite
How to find the user with the highest post created in the last 24 hours in laravel?
sorted by the number of posts in descending order.
laravel eloquent
|
show 1 more comment
up vote
2
down vote
favorite
How to find the user with the highest post created in the last 24 hours in laravel?
sorted by the number of posts in descending order.
laravel eloquent
1
highest post is interger or string?
– DPS
Nov 10 at 11:50
Highest post? What exactly do you mean by highest? Points? Comments? etc
– rpm192
Nov 10 at 11:51
Number of posts created by the user
– Nicholas Francis
Nov 10 at 11:52
Ah! Is Post a model in you application?
– rpm192
Nov 10 at 11:52
Yes! Model User has many Post. The Tricky part is i want to rank the user by the number of posts created in the last 24 hours.
– Nicholas Francis
Nov 10 at 11:53
|
show 1 more comment
up vote
2
down vote
favorite
up vote
2
down vote
favorite
How to find the user with the highest post created in the last 24 hours in laravel?
sorted by the number of posts in descending order.
laravel eloquent
How to find the user with the highest post created in the last 24 hours in laravel?
sorted by the number of posts in descending order.
laravel eloquent
laravel eloquent
edited Nov 10 at 11:44
lgwilliams
517317
517317
asked Nov 10 at 11:40
Nicholas Francis
1,18821119
1,18821119
1
highest post is interger or string?
– DPS
Nov 10 at 11:50
Highest post? What exactly do you mean by highest? Points? Comments? etc
– rpm192
Nov 10 at 11:51
Number of posts created by the user
– Nicholas Francis
Nov 10 at 11:52
Ah! Is Post a model in you application?
– rpm192
Nov 10 at 11:52
Yes! Model User has many Post. The Tricky part is i want to rank the user by the number of posts created in the last 24 hours.
– Nicholas Francis
Nov 10 at 11:53
|
show 1 more comment
1
highest post is interger or string?
– DPS
Nov 10 at 11:50
Highest post? What exactly do you mean by highest? Points? Comments? etc
– rpm192
Nov 10 at 11:51
Number of posts created by the user
– Nicholas Francis
Nov 10 at 11:52
Ah! Is Post a model in you application?
– rpm192
Nov 10 at 11:52
Yes! Model User has many Post. The Tricky part is i want to rank the user by the number of posts created in the last 24 hours.
– Nicholas Francis
Nov 10 at 11:53
1
1
highest post is interger or string?
– DPS
Nov 10 at 11:50
highest post is interger or string?
– DPS
Nov 10 at 11:50
Highest post? What exactly do you mean by highest? Points? Comments? etc
– rpm192
Nov 10 at 11:51
Highest post? What exactly do you mean by highest? Points? Comments? etc
– rpm192
Nov 10 at 11:51
Number of posts created by the user
– Nicholas Francis
Nov 10 at 11:52
Number of posts created by the user
– Nicholas Francis
Nov 10 at 11:52
Ah! Is Post a model in you application?
– rpm192
Nov 10 at 11:52
Ah! Is Post a model in you application?
– rpm192
Nov 10 at 11:52
Yes! Model User has many Post. The Tricky part is i want to rank the user by the number of posts created in the last 24 hours.
– Nicholas Francis
Nov 10 at 11:53
Yes! Model User has many Post. The Tricky part is i want to rank the user by the number of posts created in the last 24 hours.
– Nicholas Francis
Nov 10 at 11:53
|
show 1 more comment
3 Answers
3
active
oldest
votes
up vote
4
down vote
accepted
If I'm not wrong, you are asking for the users with the highest number of posts created in the last 24 hrs.
To accomplish this, do the following:
$users = User::withCount(['posts' => function ($query)
$query->where('created_at', '>=', carbon()->now()->subDay());
])->orderBy('posts_count', 'DESC')
->get();
As the documentation states, you can add constraints to the queries.
Counting Related Models
If you want to count the number of results from a relationship without actually loading them you may use the
withCount
method, which will place arelation_count
column on
your resulting models. For example:$posts = AppPost::withCount('comments')->get();
foreach ($posts as $post)
echo $post->comments_count;
You may add the "counts" for multiple relations as well as add
constraints to the queries:$posts = Post::withCount(['votes', 'comments' => function ($query)
$query->where('content', 'like', 'foo%');
])->get();
echo $posts[0]->votes_count;
echo $posts[0]->comments_count;
add a comment |
up vote
1
down vote
use CarbonCarbon;
get user id:
$minusday = Carbon::now()->subDay();
$user_id = DB::table('posts')
->select('user_id', DB::raw('count(id) as total'))
->where('created_at', '>=', $minusday)
->groupBy('user_id')
->orderBy('total','desc')
->limit(1)
->get();
add a comment |
up vote
1
down vote
In regular SQL syntax you'd need something like below:
SELECT COUNT(id), user_id
FROM posts
WHERE created_at = today
GROUP BY user_id
ORDER BY COUNT(user_id) DESC
LIMIT 1;
It gets all the posts, groups them by user_id
, sorts them with the highest user_id
count up top and gets the first record.
I am by no means an expert on SQL, let alone the query builder in Laravel, so someone else would probably be better at writing that.
I know that you can get the posts that were created today by using Carbon, like so:Post::whereDate('created_at', Carbon::today())->get();
EDIT: This might work for you:
$last24h = Carbon::now()->subDay();
DB::table('posts')
->select(array(DB::raw('COUNT(id)', 'user_id')))
->where('created_at', '>=', $last24h)
->groupBy('user_id')
->orderBy('COUNT(id)', 'DESC')
->limit(1)
->get();
Be sure to include use CarbonCarbon
to be able to use Carbon.
This should give you both the amount of posts and the corresponding user id.
it is better to link, instead of copy pasting in your answer..
– Manpreet
Nov 10 at 12:17
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
If I'm not wrong, you are asking for the users with the highest number of posts created in the last 24 hrs.
To accomplish this, do the following:
$users = User::withCount(['posts' => function ($query)
$query->where('created_at', '>=', carbon()->now()->subDay());
])->orderBy('posts_count', 'DESC')
->get();
As the documentation states, you can add constraints to the queries.
Counting Related Models
If you want to count the number of results from a relationship without actually loading them you may use the
withCount
method, which will place arelation_count
column on
your resulting models. For example:$posts = AppPost::withCount('comments')->get();
foreach ($posts as $post)
echo $post->comments_count;
You may add the "counts" for multiple relations as well as add
constraints to the queries:$posts = Post::withCount(['votes', 'comments' => function ($query)
$query->where('content', 'like', 'foo%');
])->get();
echo $posts[0]->votes_count;
echo $posts[0]->comments_count;
add a comment |
up vote
4
down vote
accepted
If I'm not wrong, you are asking for the users with the highest number of posts created in the last 24 hrs.
To accomplish this, do the following:
$users = User::withCount(['posts' => function ($query)
$query->where('created_at', '>=', carbon()->now()->subDay());
])->orderBy('posts_count', 'DESC')
->get();
As the documentation states, you can add constraints to the queries.
Counting Related Models
If you want to count the number of results from a relationship without actually loading them you may use the
withCount
method, which will place arelation_count
column on
your resulting models. For example:$posts = AppPost::withCount('comments')->get();
foreach ($posts as $post)
echo $post->comments_count;
You may add the "counts" for multiple relations as well as add
constraints to the queries:$posts = Post::withCount(['votes', 'comments' => function ($query)
$query->where('content', 'like', 'foo%');
])->get();
echo $posts[0]->votes_count;
echo $posts[0]->comments_count;
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
If I'm not wrong, you are asking for the users with the highest number of posts created in the last 24 hrs.
To accomplish this, do the following:
$users = User::withCount(['posts' => function ($query)
$query->where('created_at', '>=', carbon()->now()->subDay());
])->orderBy('posts_count', 'DESC')
->get();
As the documentation states, you can add constraints to the queries.
Counting Related Models
If you want to count the number of results from a relationship without actually loading them you may use the
withCount
method, which will place arelation_count
column on
your resulting models. For example:$posts = AppPost::withCount('comments')->get();
foreach ($posts as $post)
echo $post->comments_count;
You may add the "counts" for multiple relations as well as add
constraints to the queries:$posts = Post::withCount(['votes', 'comments' => function ($query)
$query->where('content', 'like', 'foo%');
])->get();
echo $posts[0]->votes_count;
echo $posts[0]->comments_count;
If I'm not wrong, you are asking for the users with the highest number of posts created in the last 24 hrs.
To accomplish this, do the following:
$users = User::withCount(['posts' => function ($query)
$query->where('created_at', '>=', carbon()->now()->subDay());
])->orderBy('posts_count', 'DESC')
->get();
As the documentation states, you can add constraints to the queries.
Counting Related Models
If you want to count the number of results from a relationship without actually loading them you may use the
withCount
method, which will place arelation_count
column on
your resulting models. For example:$posts = AppPost::withCount('comments')->get();
foreach ($posts as $post)
echo $post->comments_count;
You may add the "counts" for multiple relations as well as add
constraints to the queries:$posts = Post::withCount(['votes', 'comments' => function ($query)
$query->where('content', 'like', 'foo%');
])->get();
echo $posts[0]->votes_count;
echo $posts[0]->comments_count;
answered Nov 10 at 13:11
HCK
2,9651829
2,9651829
add a comment |
add a comment |
up vote
1
down vote
use CarbonCarbon;
get user id:
$minusday = Carbon::now()->subDay();
$user_id = DB::table('posts')
->select('user_id', DB::raw('count(id) as total'))
->where('created_at', '>=', $minusday)
->groupBy('user_id')
->orderBy('total','desc')
->limit(1)
->get();
add a comment |
up vote
1
down vote
use CarbonCarbon;
get user id:
$minusday = Carbon::now()->subDay();
$user_id = DB::table('posts')
->select('user_id', DB::raw('count(id) as total'))
->where('created_at', '>=', $minusday)
->groupBy('user_id')
->orderBy('total','desc')
->limit(1)
->get();
add a comment |
up vote
1
down vote
up vote
1
down vote
use CarbonCarbon;
get user id:
$minusday = Carbon::now()->subDay();
$user_id = DB::table('posts')
->select('user_id', DB::raw('count(id) as total'))
->where('created_at', '>=', $minusday)
->groupBy('user_id')
->orderBy('total','desc')
->limit(1)
->get();
use CarbonCarbon;
get user id:
$minusday = Carbon::now()->subDay();
$user_id = DB::table('posts')
->select('user_id', DB::raw('count(id) as total'))
->where('created_at', '>=', $minusday)
->groupBy('user_id')
->orderBy('total','desc')
->limit(1)
->get();
edited Nov 10 at 12:14
answered Nov 10 at 12:08
Manpreet
40016
40016
add a comment |
add a comment |
up vote
1
down vote
In regular SQL syntax you'd need something like below:
SELECT COUNT(id), user_id
FROM posts
WHERE created_at = today
GROUP BY user_id
ORDER BY COUNT(user_id) DESC
LIMIT 1;
It gets all the posts, groups them by user_id
, sorts them with the highest user_id
count up top and gets the first record.
I am by no means an expert on SQL, let alone the query builder in Laravel, so someone else would probably be better at writing that.
I know that you can get the posts that were created today by using Carbon, like so:Post::whereDate('created_at', Carbon::today())->get();
EDIT: This might work for you:
$last24h = Carbon::now()->subDay();
DB::table('posts')
->select(array(DB::raw('COUNT(id)', 'user_id')))
->where('created_at', '>=', $last24h)
->groupBy('user_id')
->orderBy('COUNT(id)', 'DESC')
->limit(1)
->get();
Be sure to include use CarbonCarbon
to be able to use Carbon.
This should give you both the amount of posts and the corresponding user id.
it is better to link, instead of copy pasting in your answer..
– Manpreet
Nov 10 at 12:17
add a comment |
up vote
1
down vote
In regular SQL syntax you'd need something like below:
SELECT COUNT(id), user_id
FROM posts
WHERE created_at = today
GROUP BY user_id
ORDER BY COUNT(user_id) DESC
LIMIT 1;
It gets all the posts, groups them by user_id
, sorts them with the highest user_id
count up top and gets the first record.
I am by no means an expert on SQL, let alone the query builder in Laravel, so someone else would probably be better at writing that.
I know that you can get the posts that were created today by using Carbon, like so:Post::whereDate('created_at', Carbon::today())->get();
EDIT: This might work for you:
$last24h = Carbon::now()->subDay();
DB::table('posts')
->select(array(DB::raw('COUNT(id)', 'user_id')))
->where('created_at', '>=', $last24h)
->groupBy('user_id')
->orderBy('COUNT(id)', 'DESC')
->limit(1)
->get();
Be sure to include use CarbonCarbon
to be able to use Carbon.
This should give you both the amount of posts and the corresponding user id.
it is better to link, instead of copy pasting in your answer..
– Manpreet
Nov 10 at 12:17
add a comment |
up vote
1
down vote
up vote
1
down vote
In regular SQL syntax you'd need something like below:
SELECT COUNT(id), user_id
FROM posts
WHERE created_at = today
GROUP BY user_id
ORDER BY COUNT(user_id) DESC
LIMIT 1;
It gets all the posts, groups them by user_id
, sorts them with the highest user_id
count up top and gets the first record.
I am by no means an expert on SQL, let alone the query builder in Laravel, so someone else would probably be better at writing that.
I know that you can get the posts that were created today by using Carbon, like so:Post::whereDate('created_at', Carbon::today())->get();
EDIT: This might work for you:
$last24h = Carbon::now()->subDay();
DB::table('posts')
->select(array(DB::raw('COUNT(id)', 'user_id')))
->where('created_at', '>=', $last24h)
->groupBy('user_id')
->orderBy('COUNT(id)', 'DESC')
->limit(1)
->get();
Be sure to include use CarbonCarbon
to be able to use Carbon.
This should give you both the amount of posts and the corresponding user id.
In regular SQL syntax you'd need something like below:
SELECT COUNT(id), user_id
FROM posts
WHERE created_at = today
GROUP BY user_id
ORDER BY COUNT(user_id) DESC
LIMIT 1;
It gets all the posts, groups them by user_id
, sorts them with the highest user_id
count up top and gets the first record.
I am by no means an expert on SQL, let alone the query builder in Laravel, so someone else would probably be better at writing that.
I know that you can get the posts that were created today by using Carbon, like so:Post::whereDate('created_at', Carbon::today())->get();
EDIT: This might work for you:
$last24h = Carbon::now()->subDay();
DB::table('posts')
->select(array(DB::raw('COUNT(id)', 'user_id')))
->where('created_at', '>=', $last24h)
->groupBy('user_id')
->orderBy('COUNT(id)', 'DESC')
->limit(1)
->get();
Be sure to include use CarbonCarbon
to be able to use Carbon.
This should give you both the amount of posts and the corresponding user id.
edited Nov 10 at 12:17
answered Nov 10 at 12:06
rpm192
1,0631417
1,0631417
it is better to link, instead of copy pasting in your answer..
– Manpreet
Nov 10 at 12:17
add a comment |
it is better to link, instead of copy pasting in your answer..
– Manpreet
Nov 10 at 12:17
it is better to link, instead of copy pasting in your answer..
– Manpreet
Nov 10 at 12:17
it is better to link, instead of copy pasting in your answer..
– Manpreet
Nov 10 at 12:17
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238563%2ffinding-a-user-with-highest-post-created-in-last-24-hours-laravel-eloquent%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
highest post is interger or string?
– DPS
Nov 10 at 11:50
Highest post? What exactly do you mean by highest? Points? Comments? etc
– rpm192
Nov 10 at 11:51
Number of posts created by the user
– Nicholas Francis
Nov 10 at 11:52
Ah! Is Post a model in you application?
– rpm192
Nov 10 at 11:52
Yes! Model User has many Post. The Tricky part is i want to rank the user by the number of posts created in the last 24 hours.
– Nicholas Francis
Nov 10 at 11:53