Post::where('created_at', '>=', now()->subDays(2))
->where(function($query) {
$query->where('published_at', '>=', now()->subDays(2))
->orWhereNull('published_at');
})->get();
Aug 15, 2022
3
Level 12
Getting all the posts in the last 2 days when having a published_at column
We're having a website that has posts in it and each post have a created_at and published_at values. created_at column is storing the actual date & time when the post is being inserted to the database. published_at is either null or a date & time which helps me determine whether the post is scheduled for a publish in the future or the post is immediately published. How can I get all the records from the last 2 days when having these 2 columns? I tried with
Post::where(function($query) {
$query->where('created_at', '>=', now()->subDays(2))
->orWhere('published_at', '>=', now()->subDays(2))
->orWhereNull('published_at');
})->get();
but it's not the expected output.
Level 51
@Laralex Limit it.
->where(function($query) {
$query->where('published_at', '>=', now()->subDays(2))
->where('published_at', '<=', now())
->orWhereNull('published_at');
})
Please or to participate in this conversation.