嗨iam初学者laravel,我有四个表用户,学生,班级,部分
用户
学生们
类
部分
当我通过使用以下方法登录获取用户名并从学生表中获取角色no时
user.php的
public function getAttribute($ key)
{$profile = student::where('stud_adm_id', '=', $this->attributes['username'])->first()->toArray();
foreach ($profile as $attr => $value) {
if (!array_key_exists($attr, $this->attributes)) {
$this->attributes[$attr] = $value;
}
}
return parent::getAttribute($key);
}
但我想从相关表中获取类名和节名也显示登录用户完整的配置文件....
并在视图页面中调用值,如下所示
<h4>ID: {{ Auth::user()->roll_no}}</h4>
<h4>Name: {{ Auth::user()->name }}</h4>
让我知道如何获得课程和部分的价值。
你可以使用Eloquent: Relationships。
在student.php中定义关系
class Student {
...
public function class() {
$this->belongsTo('App\Class');
}
public function section() {
$this->belongsTo('App\Section');
}
...
}
然后你可以在获得class
模型时检索section
和student
关系。
$student = Student::where('stud_adm_id', $username)->first();
$class = $studnet->class;
$section = $student->section;