2016-04-14 31 views
0

私は2つのモデル、製品とフィルタを持っています。Laravel belongsToMany挿入クエリのテーブル順序

製品には多数のフィルタがあり、1つのフィルタには多くの製品が含まれています。多対多の関係。

私が持っている私の製品で

public function filters() { 
    return $this->belongsToMany('App\Models\Filter'); 
} 

そして、私のフィルターで:

public function products() { 
    return $this->belongsToMany('App\Models\Product')->withTimestamps(); 
} 

私は製品を挿入し、このようなフィルタを保存した後にしよう:

$filtersId = $request->filter; 
$product->filters()->attach($filtersId); 

このエラーが発生しました:

Base table or view not found: 1146 Table 'pontocom-moveis.filter_product' doesn't exist (SQL: insert into `filter_product` (`filter_id`, `product_id`) values (1, 31)) 

どのように注文をproduct_filterに変更できますか?

答えて

1

の方法の2番目のパラメータはテーブル名です。あなたが使用する必要があり、この場合はそう

public function filters() { 
    return $this->belongsToMany('App\Models\Filter', 'product_filter'); 
} 

public function products() { 
    return $this->belongsToMany('App\Models\Product', 'product_filter')->withTimestamps(); 
} 
0

あなたは、テーブルを作成する必要があります。属性を持つfilter_product

filter_idproduct_id

+0

私はすでにこの関係のためにテーブルを作成しました。私は、belongsToManyに関係の名前を渡すのを忘れていました。ありがとう。 –

関連する問題