Reputation: 2174
I am sure this is because of the eloquent, but the query is using the wrong table in SELECT
.
here my model:
class Subcategory extends Model
{
public function subcategory()
{
return $this->belongsTo(Category::class);
}
}
and here is my Controller:
class SubcategoriesController extends Controller
{
public function index()
{
$subcategories = Subcategory::all();
return view('pages.subcategories', compact('subcategories'));
}
}
and finally here my error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'developmnet.site.subcategories' doesn't exist (SQL: select * from `subcategories` where `category_slug` = budgets limit 1)
as you can see from the error the table referenced in the query is subcategories
when it should be categories
table.
My question is how to reference the correct table in this model as I already have one for my main categories and it is working just fine.
Upvotes: 0
Views: 1991
Reputation: 31772
To overwrite the table name of a model you can define the protected $table
property. In Your case:
class Subcategory extends Model
{
protected $table = 'categories';
// ..
}
If you don't do that, Laravel will derive the table name from the class name. In your case: Subcategory => subcategories
.
Upvotes: 3