0

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.

1 Answer 1

2

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();
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.