damcclean
11 months ago
146
22
Eloquent

Can't seem to get my Eloquent model working

Posted 11 months ago by damcclean

Hi,

I'm having trouble with my Eloquent models in my Laravel 5.7 app.

I'm trying to set it up so that a Subject has a level and a level has many subjects.

This is in my Subject model

public function level() {

        return $this->belongsTo('App\Subject');
        
    }

And this is in my level model.

public function subject() {

        return $this->hasMany('App\Subject');
        
    }

When I try in my all subjects blade view to use the title of a level I get the following error. Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$title

My code inside my blade view is the following:

<td>{{ $subject->level()->title }}</td>

Am I doing this correctly or is there something wrong?

Duncan

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