mercredi 1 mars 2017

Laravel model referencing wrong table in query

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.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire