A
A
andrew-kondaurov2021-12-06 17:43:00
PostgreSQL
andrew-kondaurov, 2021-12-06 17:43:00

How to change a record in the database when a certain date is reached?

I want to enable monthly subscription on a website (laravel 8).
I have a table in the database that defines the subscription expiration date and the subscription status (1 or 0).
The subscription status determines access to the content.

61ae1f213f62f220624243.png

How can I write a function, and what should I use so that when a certain date is reached, the subscription status changes to 0 in the database?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
FanatPHP, 2021-12-06
@FanatPHP

Databases don't work that way.
In this case, the status is redundant, and it can be removed from the database
. Thus, nothing will need to be changed.

S
Slava Rozhnev, 2021-12-06
@rozhnev

As FanatPHP wrote , the status field is redundant,
if you need to have the current subscription status, you can create a view:

create view subscriptions_status as select 
  id, user_id, created_at, end_at,
  case when end_at > now() then 1 else 0 end active
from subscriptions;

select * from subscriptions_status;

PostgreSQL view online

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question