Home>

I want to know where and how to write processing that is not related to user operation in laravel.

Specifically, after a certain period of time has passed for an article posted by a user, we are thinking of keeping the number of comments and ratings for that article in a table separate from the article table. Also, does the process change depending on whether the fixed time is a few minutes or a few days?

Another question, if you're using eloquant and want to get the last row of a questions table (the table's primary key is an auto-increment id, assuming you have a huge number of records), the following statement came up. However, I felt that there was a problem. I would like to know a good way.

Controller description

$question = Question :: orderBy ('id','desc')->first ();
Is the query slow because id is in ascending order on the database?

$question = Question :: all ()->last ();
Squeeze memory to get all records once?

  • Answer # 1

    If the process after a certain period of time runs only once (1 hour, 1 day, 1 week, no further process), put it in the Queue on the controller.
    Scheduling with Cron (https://readouble.com/laravel/8.x/ja/scheduling.html for Laravel) if you want to keep updating every day regardless

    So, in both cases, create a job and write the process there

    $question = Question :: orderBy ('id','desc')->first ();
    Is the query slow because id is in ascending order on the database?

    If you are making a table according to laravel's method, it will be only spec shoboy. SQL tuning would be useless. If you created it yourself, review createSQL.

    $question = Question :: all ()->last ();
    Squeeze memory to get all records once?

    Of course