0

Suppose a situation in which, I have to run a command php artisan foo:bar. In my command I have something like this:

public function handle()
{
    // Some Query to the database using 'pluck'    
    $valuesFromDatabase = ['aaa', 'bbb'];
    dd($valuesFromDatabase);
}

The first time this will lead to ['aaa', 'bbb']. Okay, now after 5 minutes there are some changes on database and the command will be run and the result would be like this:

public function handle()
{
    // Some Query to the database using 'pluck' (same query as last time)    
    $valuesFromDatabase = ['aaa', 'ccc', 'ddd'];
    // Something I should do here.
    dd($valuesFromDatabase);
}

After the second time the command runs, result will be ['aaa', 'ccc', 'ddd'], BUT I want the result to be like this ['aaa', 'bbb', 'aaa', 'ccc', 'ddd'].
What should I do to store the values from the first time of running the command, beside the second time of running the command and have the values from the last time running the command? I need a way other than database, and tried with session but it renews after running the command for the second time.

Majid Alaeinia
  • 962
  • 2
  • 11
  • 27
  • Do you mean permanently store them as `['aaa', 'bbb', 'aaa', 'ccc', 'ddd']` or just for as long as the script runs? – webnoob Dec 10 '20 at 14:19
  • You really should be thinking about your CLI as being part of your application, not adjacent to it. What I mean by this is that if your CLI needs those values, your application needs them, too, because it is part of it. You should find a way to overall flag those value in a different way so that your CLI and of your application can all work together. – Chris Haas Dec 10 '20 at 14:23
  • @webnoob I want them while running the artisan command for the second time, the result from the last time running the command, being merged with the result from the current ruinng command and decide on it while running the command. – Majid Alaeinia Dec 10 '20 at 16:10
  • @ChrisHaas I think the key answer is related to answering what happens to the session, running thread and things like that on running an artisan command and what happens to them after the command is finished. – Majid Alaeinia Dec 10 '20 at 16:24
  • The concept of a "session" [doesn't really](https://stackoverflow.com/a/39669116/231316) [make sense](https://stackoverflow.com/a/29033007/231316) in a CLI context. But if you want sessions, you are going to have to hack something together. Sessions in PHP are created using cookies by default, and since there's no HTTP request from the CLI, there's no cookie to pass the session ID. You are going to need to find a way to fake a cookie system (basically store the cookie on disk your self) and then find a way to load the session data from that cookie. – Chris Haas Dec 10 '20 at 17:15
  • @ChrisHaas That's right since no HTTP request is triggered, but I cannot think of anything but a database or session to store data, and searching if there's a way to store something, somewhere but a database. I am thinking of writing the values to a file and decide on their occurrence since they are less than four items every five minutes and there's no need to use database for storing them. – Majid Alaeinia Dec 10 '20 at 21:02
  • Pretty much every web app that I've built for the past couple of years has some form of CLI, so when I say that you should try to keep the state in the database somehow, I say that with experience. But if that's not an option for you, I understand. Given those restrictions, I'd just write things to a quick JSON file and be done with it. The greatest worry is concurrency which you can solve through locking, and what happens if the CLI loses the JSON, which the database would solve but you'll need to find your own method. – Chris Haas Dec 11 '20 at 00:37
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/225809/discussion-between-majid-alaeinia-and-chris-haas). – Majid Alaeinia Dec 11 '20 at 00:50

0 Answers0