I am new to laravel and im stuck with my relationshops what looks like the following
Categories
id name slug
-----------------------------------------------------------------
3 Location location
4 Outfits outfits
5 Other other
sub_categories
id category_id name slug
-----------------------------------------------------------------------------
12 3 Club club
13 3 Home / Hotel home-hotel
14 3 Outdoor outdoor
15 3 Studio studio
16 4 Bikini / Swimwear bikini-swimwear
17 4 Dress dress
19 4 Jeans jeans
35 5 Dancing dancing
Category model
<?php
class Category extends Eloquent {
public $timestamps = false;
public function subcategory()
{
return $this->belongsToMany('subcategory', "sub_categories");
}
}
And i get the following error
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'sub_categories' (SQL: select `sub_categories`.*, `sub_categories`.`category_id` as `pivot_category_id`, `sub_categories`.`subcategory_id` as `pivot_subcategory_id` from `sub_categories` inner join `sub_categories` on `sub_categories`.`id` = `sub_categories`.`subcategory_id` where `sub_categories`.`category_id` = ?) (Bindings: array ( 0 => 1, ))
Could please someone point out what I am doing wrong?
sub_categoryat once. A many-to-many relationship requires 3 tables (category/subcategory/category_subcategory) which need distinct names. If two of the tables get the same name, you'll get the error you're showing.