简体   繁体   English

如何使用laravel where子句和created_at从数据库中选择

[英]How to select from database using laravel where clause and created_at

I want to select all the data on a database using laravel db query or Eloquent ORM. 我想使用laravel db query或Eloquent ORM选择数据库上的所有数据。 So i want all the data that are other than 3 days from the day they where created. 所以我想要从创建之日起3天以外的所有数据。 Data 'A' is created on 15th i come in on 16th to check, i won't see Data 'A' but i may see others older than 3 days or equal to 3 days. 数据“ A”在15日创建,我在16日进入检查,我看不到数据“ A”,但我可能会看到其他早于3天或等于3天的数据。 If i come back on the 18th or 19th i should see Data 'A', obviously because it now older than 3 days or equal to. 如果我回到18号或19号,应该会看到数据“ A”,显然是因为它现在早于3天或等于3天。 so i wrote this code that is not working below 所以我写了下面的代码

$users = DB::table('matched_users')->where( 'created_at', '>=',
 Carbon::now()->subDays(3)); 

so can anyone correct this for me. 任何人都可以为我更正此错误。

when i did that it work but it shows both someone that his date is just one day one. 当我做到这一点时,它同时向两个人表明他的约会只是一天的一天。 so is that not enough code 所以代码不够吗

Data Name - created_at
ozil        21/04/2012 16:09:22
mark        21/04/2012 16:09:22
cyril       22/04/2012 16:19:21

so today is 25/04/2012 if run the query of run the above query as it is i get all the result back. 所以今天是25/04/2012,如果运行运行上述查询的查询,因为它是我得到的所有结果。

But if change the >= to <= the result is an empty collection 但是,如果将>=更改为<=则结果为空集合

I guess you mean 'older than 3 days from the day they where created'. 我猜您的意思是“自创建之日起3天以上”。 In this case the query should look like this: 在这种情况下,查询应如下所示:

DB::table('matched_users')->where('created_at', '<=', Carbon::now()->subDays(3)); 
$users = DB::table('matched_users')->whereDate( 'created_at', '<=', Carbon::now()->subDays(3))->get();

是编写代码的正确方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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