-1

I have an user model and a student model which I have created relationship for, but when I try to

$student->user->fullname

I get this error

"trying to get property fullname of non-object"

here is my user model code:

<?php

namespace App;


use App\Assignment;
use App\Model\Quiz;
use App\Model\Course;
use App\Topic;
use App\Model\Guardian;
use App\Model\Student;
use App\Model\Teacher;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable, HasRoles, SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'fullname',
        'email',
        'avatar',
        'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function setPasswordAttribute($password)
    {
        $this->attributes['password'] = bcrypt($password);
    }



    public function guardian()
    {
        return $this->belongsTo(Guardian::class);
    }

    public function teacher()
    {
        return $this->belongsTo(Teacher::class);
    }

    public function student()
    {
        return $this->belongsTo(Student::class);
    }

    public function assignments()
    {
        return $this->hasMany(Assignment::class);
    }

    public function quizzes()
    {
        return $this->hasMany(Quiz::class);
    }

    public function courses()
    {
        return $this->hasMany(Course::class);
    }

    public function topics()
    {
        return $this->hasMany(Topic::class);
    }

    public function levels()
    {
        return $this->hasMany(Level::class);
    }
}

and here is my student model code

<?php

namespace App\Model;

use App\User;
use App\Model\Course;
use App\Assignment;
use App\Level;
use App\Model\DoneQuiz;
use App\Model\Teacher;
use App\Model\Guardian;
use Illuminate\Database\Eloquent\Model;

class Student extends Model
{
    protected $fillable = ['user_id', 'level_id', 'guardian_id'];

    public function user()
    {
        return $this->belongsTo(User::class);
    } 

    public function courses()
    {
        return $this->hasMany(Course::class);
    } 

    public function assignments()
    {
        return $this->hasMany(Assignment::class);
    }

    public function level()
    {
        return $this->hasOne(Level::class);
    }

    public function teachers()
    {
        return $this->hasMany(Teacher::class);
    }

    public function guardian()
    {
        return $this->hasOne(Guardian::class);
    }

    public function donequizzes()
    {
        return $this->hasMany(DoneQuiz::class);
    }
}

and even when I try to use this relationship to get data like

'student_id' => auth()->user()->student()->id

I get this error

"BadMethodCallException Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsTo::id()"

popcorn
  • 388
  • 1
  • 7
  • 28
  • It seems that the user is not actually associated with a student. can you share your table structures? Also `belongsTo` is an inverse of a `hasMany` or `hasOne` relationship. You have `belongsTo` on both sides of the user-student relationship – apokryfos Jan 08 '20 at 08:25

1 Answers1

2

when you use student() it returns a query builder

Either change it to simple student

'student_id' => auth()->user()->student->id

OR

 'student_id' => auth()->user()->student()->first()->id
FULL STACK DEV
  • 15,207
  • 5
  • 46
  • 66