B
B
BonBon Slick2016-12-26 14:27:27
Laravel
BonBon Slick, 2016-12-26 14:27:27

Save Many to Many Laravel 5.3?

There is a Pivot table, where the product id and tag id.
controller:

$tagsNames = ['1', '2', '3'];
  $product->productTags->saveMany([
      new Tag(['tag_id' => '1']),
      new Tag(['tag_id' => '2']),
      new Tag(['tag_id' => '3']),
    ]);

Model:
public function productTags()
    {
        return $this->belongsToMany('App\Tag', 'product_tags');
    }
// тоже самое только наоборот в модели Tag

Method saveMany does not exist.
Tried sync method doesn't exist either.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Arman, 2016-12-26
@BonBonSlick

at least
$product->productTags != $product->productTags()

V
Vlad, 2016-12-26
@Result007

Maybe try attach together with saveMany?

E
Eugene, 2016-12-26
@Nc_Soft

$product->productTags()->saveMany()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question