【问题标题】:How to reach to another column field using auth()->user()->student()->registration->student_id?如何使用 auth()->user()->student()->registration->student_id 访问另一个列字段?
【发布时间】:2020-04-21 20:45:47
【问题描述】:

我有 3 个表“用户”、“学生”和“注册”。每个表都与 hasOne 关系链接。我想从班级表中获取student_id的值。是否可以找到?其实我是新人,我不明白

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

//错误 Undefined property: Illuminate\Database\Eloquent\Relations\HasOne::$registration

【问题讨论】:

  • 我从 student 中删除了 () 。它显示一个错误。是否可以访问另一个具有关系的表字段?

标签: php laravel authentication


【解决方案1】:

如果学生有过一种关系而不是使用

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

只需删除学生后面的圆括号即可。

【讨论】:

    【解决方案2】:

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

    @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])}}
    

    【讨论】:

      【解决方案3】:

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

      正确的代码是

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-08
        • 2018-06-14
        • 2016-01-17
        • 2022-11-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多