【问题标题】:how do i go getting this eloquent relationship right?我如何才能得到这种雄辩的关系呢?
【发布时间】:2020-04-25 17:59:38
【问题描述】:

我有一个用户模型和一个学生模型,我已经为其创建了关系,但是当我尝试这样做时

$student->用户->全名

我收到这个错误

"试图获取非对象的属性全名"

这是我的用户模型代码:

<?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()"

【问题讨论】:

  • 似乎用户实际上并没有与学生关联。你能分享你的表结构吗?同样belongsTohasManyhasOne 关系的逆。你在用户和学生关系的两边都有belongsTo

标签: php laravel eloquent eloquent-relationship


【解决方案1】:

当你使用 student() 时,它会返回一个查询构建器

要么把它改成简单的学生

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

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

【讨论】:

  • "试图获取非对象的属性 'id'"
  • @AdewaleCharles 好像列中的相关数据不存在
猜你喜欢
  • 2017-07-23
  • 2013-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
相关资源
最近更新 更多