Create a daily counter with php in Laravel application

Viewed 51

In my web application, created with Laravel 8, I need to generate file names with a specific nomenclature and add a progressive day number to the end of the name, and this progressive number is incremented by 1 every time a file is created. The example nomenclature is $filename = 'filename'.$counter;.

So I thought of creating inside my controller, a variable defined as an array with 'key' => 'value' like this $counter = [date("Y-m-d") => 0];

So in the method I use to create the files, public function creaFile(Request $request) check if the key of $counter is equal to today's date or not. My problem is to check if a day has passed, so if today's date is different from what the $counter key represents, then reset the counter.

Do you have any suggestions or ideas on how to improve this?

  • my code

class CreaFileController extends Controller
{
private $counter = [ date("Y-m-d") => 0];
private $filename = 'my_file';

public function index(){
   //
}

//here i need to use the $counter
public function creaFile(Request $request)
{
   // here i need to check if today date is equal to $counter key
   if(array_keys($this->counter) == date("Y-m-d")){
      // i need to increase the value of $counter by 1
      $this->createNewFile($this->filename, $counter);
   }else{
     // i need to reset the $counter to 0 and change the value of the key
   }
}

}

0 Answers
Related