How can I insert in the DB 100 tickets numbered from 001 to 100?

Posted 4 months ago by P-Torres

Hi,

Please, I need some advice. I'm building a raffle site.

I have two tables, "competitions" and "tickets" with a "One To Many" relationship.

The CMS allows the administrator to create the competitions, all good.

The CMS allows the administrator to select a competition and "generate" "x" number of tickets, here my problem.

How can I, eg. insert in the DB 100 tickets numbered from 001 to 100?

Please, any advice, starting point, idea, sample, etc is much appreciated, Thank you!

Currently, my controller looks like:

public function store(Request $request)
{

    //eg. 100 tickets coming from the form input
    $tickets_quantity = $request->input('tickets_quantity');

    //How to get the below automated implementing $tickets_quantity?
    $tickets = [
        ['competition_id'=>1, 'number'=>001, 'price'=>120, 'is_available'=>true, 'created_at'=>NOW(), 'updated_at'=>NOW()],
        ['competition_id'=>1, 'number'=>002, 'price'=>120, 'is_available'=>true, 'created_at'=>NOW(), 'updated_at'=>NOW()],
        //...
    ];

    Ticket::insert($tickets);

    return redirect()->route('tickets.index')->with('status', 'Success');
}

Please sign in or create an account to participate in this conversation.