A
A
Alexander Shapoval2018-02-28 00:41:00
Laravel
Alexander Shapoval, 2018-02-28 00:41:00

How to attach a list of accounts to the list of products?

There is a model with a method

public function instagrams() {
        return $this->belongsToMany(AccountInstagram::class, 'products_instagrams');
    }

to add acc to the product I call
$product->instagrams()->attach($accounts->pluck('id'));

how to do this not to one product but to a list? something like this
$products->instagrams()->attach($accounts->pluck('id'));

so you don't have to loop
foreach ($products as $product) {
    $product->instagrams()->attach($accounts->pluck('id'));
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Semasping, 2018-03-08
@Semasping

I think eager loading can help here, when the data from the connection is requested immediately.
https://laravel.com/docs/5.4/eloquent-relationship...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question