PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 5, 2022

[FIXED] Laravel - Show users from the last 30 days

 January 05, 2022     laravel, laravel-5     No comments   

Issue

I have a Laravel query that looks like this

$users = DB::table("users")
                   ->select('id')
                   ->where('accounttype', 'standard')
                   ->get()->all();

It is working and returning the id for all standard accounts, I am trying to limit it to only return results from the last 30 days, the date the user was created is stored as a timestamp in 'created_at'

Is it best to do this in the query or should I process the results afterwards?


Solution

You can use carbon along with the where clause:

use Carbon\Carbon;

$users = DB::table("users")
    ->select('id')
    ->where('accounttype', 'standard')
    ->where('created_at', '>', now()->subDays(30)->endOfDay())
    ->all();

As noted in the comments, do as much in the query as possible until you notice performance issues or your queries become unreadable.



Answered By - Chris
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing