Trying to get property of non-object in laravel 5.3 app though model relations s...

User model

class User extends Authenticatable{
    public function enrollments() {
    return $this->hasMany('App\enrollments','user_email');
}}

Batch model

class batch extends Model{
    protected $table = 'batch';
    public function enrollments() {
        return $this->hasMany('App\enrollments');
    }}

enrollments model

class enrollments extends Model{
    public function batch() {
        return $this->belongsTo('App\batch');
    }
    public function user() {
        return $this->belongsTo('App\User','email');
    }}

if I use $enrollment->batch->title, it works.. but if I use $enrollment->user->name, it gives an error

Trying to get property of non-object

Please help, I am stuck Thanks in advance

sgsahilgupta
sgsahilgupta
  • 6 days ago

You User and Enrollment relationships seems to be wrong ?

class enrollments extends Model{
    public function user() {
        return $this->belongsTo('App\User','email_column_of_enrollments_model', 'email_column_of_user_model');
    }}
}

class User extends Authenticatable{
    public function enrollments() {
    return $this->hasMany('App\enrollments','email_column_of_user_model','email_column_of_enrollments_model');
}}