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.










share|improve this question



















  • 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














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.










share|improve this question



















  • 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












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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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












  • 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












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 a relation_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;






share|improve this answer



























    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();





    share|improve this answer





























      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.






      share|improve this answer






















      • it is better to link, instead of copy pasting in your answer..
        – Manpreet
        Nov 10 at 12:17










      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      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

























      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 a relation_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;






      share|improve this answer
























        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 a relation_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;






        share|improve this answer






















          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 a relation_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;






          share|improve this answer














          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 a relation_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;







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 13:11









          HCK

          2,9651829




          2,9651829






















              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();





              share|improve this answer


























                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();





                share|improve this answer
























                  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();





                  share|improve this answer














                  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();






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 10 at 12:14

























                  answered Nov 10 at 12:08









                  Manpreet

                  40016




                  40016




















                      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.






                      share|improve this answer






















                      • it is better to link, instead of copy pasting in your answer..
                        – Manpreet
                        Nov 10 at 12:17














                      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.






                      share|improve this answer






















                      • it is better to link, instead of copy pasting in your answer..
                        – Manpreet
                        Nov 10 at 12:17












                      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.






                      share|improve this answer














                      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.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      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
















                      • 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

















                      draft saved

                      draft discarded
















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                      Syphilis

                      Darth Vader #20