Search code examples
phplaravelemailgenerateticket-system

How to generate report ticket number with Laravel?


Currently working on lost and found project where users can make a report when they left something behind.

I'm using laravel 5.4, I have some data already on my DB and I have made every requirements according to my client. But in the end, my client want me to add unique report number ticket for every report that has been made. I've googled it but I can't find tutorial that is similar to my case

I'm totally newbie on programming, any help would be much appreciated.


Solution

  • There's a lot of ways you can do so, but the following two sprint to mind:

    1. Use an auto-increment column from your database and add some other stuff to it like the current date and make sure to add a few zeros in there (e.g. 20192208-00001).
    $date = new DateTime('now');
    echo $id = $date->format('Y-m-d') . "-" . str_pad($ticket->id, 6, "0", STR_PAD_LEFT);
    
    1. Alternatively you can use a Uuid for this. These are uniquely generated numbers with a near impossible chance of collision. To set this up you'll need a library which supports it composer require ramsey/uuid. Then you can generate your random numbers like so:
    $uuid4 = Uuid::uuid4();
    echo $uuid4->toString(); // i.e. 25769c6c-d34d-4bfe-ba98-e0ee856f3e7a