10 months ago

Eloquent belongsTo() returning null

Posted 10 months ago by ihassan

Don't know what is wrong with the relationship but I am unable to get relationship data.

Below is the code.

##Category Model:

    public function subjects()
        return $this->hasMany(Subject::class);

##Subject Model:

    public function category()
        return $this->belongsTo(Category::class);

In my controller I did this,

$records = Subject::with('category')->find(1);

but it always returns null and I am unable to get category name for specific subject. I have checked my database and can see that records exists there.

Any help will be really appreciated. I know this is a very simple thing but really unable to spot the error!

Please sign in or create an account to participate in this conversation.