如何使用 auth()->user()->student()->registration->student_id 访问另一个列字段?



我有 3 个表"用户"、"学生"和"注册"。每个表都与 hasOne 关系相关联。我想从类表中获取student_id的值。有可能找到吗?其实我是新来的,我没有得到那个

//法典<a href="{{route('gallary.view',['stud_id' =>auth()->user()->student()->registration->student_id])}}">

//错误Undefined property: IlluminateDatabaseEloquentRelationsHasOne::$registration

如果学生有过一段关系,则使用

{{route('gallary.view',['stud_id' =>auth()->user()->student->registration->student_id])}}

只需在学生之后删除圆括号即可。

如果你正确地定义了你的关系,你可以做这样的事情。

@php
$user_details = User::('student.registration')->where('id',auth()->user()->id)->first();
$student_id = $user_details->student->registration->student_id;
@endphp
{{route('gallary.view',['stud_id' =>$student_id])}}

您应该从学生关系中删除()

正确的代码是

<a href="{{route('gallary.view',['stud_id' =>auth()->user()->student->registration->student_id])}}"

最新更新