我该如何建立这种雄辩的关系?

问题描述 投票:0回答:1

我有为之创建关系的用户模型和学生模型,但是当我尝试$ student-> user-> fullname时。我收到此错误“试图获取非对象的属性全名”这是我的用户模型代码:

<?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);
    }
}

这是我的学生型号代码

<?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);
    }
}

甚至当我尝试使用这种关系来获取像]这样的数据时>

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

我收到此错误“ BadMethodCallException调用未定义的方法Illuminate \ Database \ Eloquent \ Relations \ BelongsTo :: id()”

我有为之创建关系的用户模型和学生模型,但是当我尝试$ student-> user-> fullname时。我收到此错误“试图获取非对象的属性全名”,这是我的...

php laravel eloquent eloquent--relationship
1个回答
0
投票

当您使用student()时,它返回一个查询生成器

© www.soinside.com 2019 - 2024. All rights reserved.