I have two models, Product and Filter.
A product can have many filters and one filter can have many products. Many to Many relationship.
In my Product I have:
public function filters () {
return $this->belongsToMany('App\Models\Filter');
}
And in my Filter:
public function products () {
return $this->belongsToMany('App\Models\Product')->withTimestamps();
}
When I try to insert a product and after save the filters like this:
$filtersId = $request->filter;
$product->filters()->attach($filtersId);
I've gotten this error:
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))
How can I change the order to product_filter?
The second parameter of the method is the table name.
So in this case you should use:
public function filters () {
return $this->belongsToMany('App\Models\Filter', 'product_filter');
}
public function products () {
return $this->belongsToMany('App\Models\Product', 'product_filter')->withTimestamps();
}