"newerThan" would be a >=, not a <= :) You accidentally programmed "olderThan"
Nov 1, 2019
6
Level 4
Query returning unexpected results
In my application I have a Spotlight model.
I run this query:
Spotlight::newerThan(1)->published()->inRandomOrder()->take(2)->get();
Here are the query scopes.
/**
* Scope an article by whether or not it's published
*
*/
public function scopePublished($query)
{
return $query->where('status', 'published');
}
/**
* Only retrieve Spotlights newer than the given increment
*
* @param [type] $query
* @param [type] $interval
* @return void
*/
public function scopeNewerThan($query, $interval)
{
return $query->where('created_at', '<=', Carbon::now()->subMonths($interval)->toDateTimeString());
}
Dumping the actual query gives this:
"select * from `spotlights` where `created_at` <= 2019-10-01 16:14:27 and `status` = published order by RAND() limit 2"
However, I have no spotlights that are newer than one month, and yet it still returns something.
Level 28
1 like
Please or to participate in this conversation.