Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Jeyziii's avatar

how to hide post automatically

I have published_at and expired_at in my articles table. Here is my controller

$latest_article=Article::whereDate('published_at', '<=', Carbon::now())
->latest('published_at')
->limit(5)
->get();

After posting the article on the specific time and date, I need to hide it if expired_at column has a value. I just want to be removed in the view, not delete it.

Is there a way where I can just chain the hide part? Or it will be on a separate function?

0 likes
11 replies
vincent15000's avatar
$latest_article=Article::whereDate('published_at', '<=', Carbon::now())
	->whereNotNull('expired_at')
	->latest('published_at')
	->limit(5)
	->get();
vincent15000's avatar

@Jeyziii Probably because your expired_at field is null ?

Can you show the content of your articles table in the database with an example of article that should not be shown ?

1 like
vincent15000's avatar

@Jeyziii Sorry I just noticed that I had something wrong in my proposition of code.

You need to hide the articles which have an expiration date.

$latest_article=Article::whereDate('published_at', '<=', Carbon::now())
	->whereNull('expired_at') // so you show only where the expiration date is null
	->latest('published_at')
	->limit(5)
	->get();

If you need to check the expiration date to be less than the actual date, you need to add a condition around the expiration date.

1 like
Jeyziii's avatar

@vincent15000 Sorry, my construction of sentence is wrong. What I mean is, I want the article to be hidden at the date and time set in the expired_at. Like after the article is posted, it will not shown in the front end when the date and time comes.

1 like
vincent15000's avatar
Level 63

@Jeyziii That's totally different.

use Carbon\Carbon;
...
$latest_article=Article::whereDate('published_at', '<=', Carbon::now())
	->whereDate('expired_at', '>', Carbon::now())
	->latest('published_at')
	->limit(5)
	->get();
1 like
Jeyziii's avatar

@vincent15000 Yes, I'm sorry I hadn't slept yet when I composed the question. Thank you very much. I will try this

1 like
saedyousef's avatar

@vincent15000

->whereDate('expired_at', '>', Carbon::now())

not

->whereDate('expired_at', '<', Carbon::now())
2 likes
Jeyziii's avatar

Thank you very much. It is now Okay.

1 like

Please or to participate in this conversation.