Search code examples
phplaravelfor-looplaravel-nova

Laravel Nova create same record multiple times


I am stuck with a issue on Laravel. I like to insert a records multiple times. The user can add a amount, lets say 3. Than the insert query has to run 3 times.

I was able to make a observer but when i loop through there, it will still add one.

See the code below:

public function creating(CardOrder $cardOrder)
{
    if($amount = $cardOrder->amount) {
        unset($cardOrder->amount);
        for($i = 0; $i < $amount; $i ++) {
            $cardOrder->entity_id = 1234;
            $cardOrder->group_id = 'test';
        }
    }
}

Is there a way to do that, and do i need a observer to accomplish that?

Thank you in advance.


Solution

  • Just added them $cardOrder = new CardOrder;

      public function creating(CardOrder $cardOrder)
      {
        if($amount = $cardOrder->amount) {
            unset($cardOrder->amount);
             $cardOrder =  new CardOrder;
            for($i = 0; $i < $amount; $i ++) {
                $cardOrder->entity_id = 1234;
                $cardOrder->group_id = 'test';
                $cardOrder->save();
            }
        }
    }