-2

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.

ellvina
  • 17
  • 8
  • I suppose you have an auto increment column for tickets in your database? These are unique numbers. – Jerodev Aug 22 '19 at 07:17
  • yeah, I have. But the client asked for more complicated number like #466244658574 to be attached on the email confirmation sent to the user after they submitted the report. I'm actually confused xD – ellvina Aug 22 '19 at 07:26

2 Answers2

0

You can use Unix Timestamp as your unique filed.

$token = time();

Now add this token when you are saving a new report to database. If you want to make it more unique you can add some more random strings to it like

In Laravel 5.8 You need to use use Illuminate\Support\Str; this first. and then

$token = time().Str::random(5);

In previous versions its like

$token = time().str_random(5);
zahid hasan emon
  • 6,023
  • 3
  • 16
  • 28
0

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
PtrTon
  • 3,705
  • 2
  • 14
  • 24