user3763103
user3763103

Reputation: 111

Saving issue in laravel 5.4

I'm trying to save multipliable ids in the database using loop but it only save the last id 3 times so I made for loop to test it

 for ($i = 0; $i < 10; $i ++)
    {
        $this->product_id = $i;
        $this->shop_name  = $shop;
        $this->save();
    }

it saves number 9 three times and stops ????

This issue never happened before

Upvotes: 2

Views: 47

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163838

You need to create a new instance with each iteration, for example:

for ($i = 0; $i < 10; $i++) {
    $this->create(['product_id' => $i, 'shop_name' => $shop]);
}

Your code updates a single instance multiple times.

Upvotes: 4

Related Questions