'How generate receipt number with range and validation

How to create a unique receipt number in the following range: T.QA-00001 - T.QA-99999 without any duplication

I tried to make like this and it's not working

        $prefix = 'T.QA-';
    do {
        $new = str_pad(rand(1,99999),5,'0',STR_PAD_LEFT);
        $new = strval($new);
        $receipt_number = $prefix.$new;
    } while (Insurance::where("receipt",$receipt_number)->exists());

    $data = Insurance::create([
        'receipt' => $receipt_number,])

So please help me out.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source