【问题标题】:how to use "belongTo" relationship in larvael如何在 laravel 中使用“belongsTo”关系
【发布时间】:2016-06-01 19:36:28
【问题描述】:

我有两个模型“用户”和“医生”,我想使用 eloquent relatonahiops hasMany 和belongaTo 将它们关联如下

This is the model for users
   protected $fillable = [
    'name',
    'email',
    'password',
    'role_id',
    'active_user',
];

public function doctor()
{
    return $this->hasMany('App\Doctor','user_id');
}

医生的模型如下 受保护的 $table="医生";

protected $fillable = [
    'role_id',
    'user_id',
    'name',
    'date',
    'gender',
    'specialization',
    'state',
    'country',
    'email',
    'mobile',
    'phone',
    'address',
    'password',
    'pic'

];
public function user()
{
    return $this->belongsTo('User','user_id');
}

现在我有一个表格,我在下面使用@foreach 语句显示医生

<table id="example" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
    <thead >
    <tr >
        <th>
            S/N
        </th>
        <th>
            Picture
        </th>

        <th style="text-align: center;">
            Name
        </th>

        <th style="text-align: center;">
            Specialization
        </th>

        <th  style="text-align: center;">
            Phone Number
        </th>

        <th  style="text-align: center;">
            Status
        </th>


        <th style="text-align: center;">
            Book
        </th>
        {{--<th style="text-align: center;">--}}
        {{--Message--}}
        {{--</th>--}}

    </tr>
    </thead>
    <tbody>
    <?php $c=0;?>
    @foreach($doctors as $dr)
        <tr style="padding: 10px; text-align: center;">
            <td>{{++$c}}</td>
            <td>
                <img src='{{url("assets/images/doctors_pic_uploads/".$dr->pic)}}' style="height:40px; width:40px;" class="img-circle">
            </td>
            <td style="padding: 20px;">{{$dr->name}}</td>
            <td style="padding: 20px; ">{{$dr->specialization}}</td>
            <td style="padding: 20px;">{{$dr->phone}}</td>
            <td style="padding: 20px;">

            </td>
            <td style="padding: 20px;">
                <input type="radio" name="book" value="{{$dr->id}}">

            </td>
           This is where i want to show if they are offline or online
        </tr>
    @endforeach


    </tbody>
</table>

我的控制器在下面

public function BookDoctors(){
    if(!Auth::check()) {
        return Redirect::to('admin/login');
    }
    $opt=User::findOrFail(Auth::User()->id)->operator->first();
    $doctors = Doctor::all();
    $patients = PatientInfo::all();
    return view('Others.Operator.Appointment.book_doctors')->with(compact('opt','doctors','patients'));

}

现在的挑战是我想从用户迁移中获取“active_user”字段,并将其包含在 laravel 中使用 belongsTo 关系的具有“状态”表头的表单

【问题讨论】:

  • 你应该可以像$doctor-&gt;user-&gt;active_user这样访问它。你试过了吗?
  • 试过不行
  • 为什么它不起作用?你收到错误消息还是什么?任何信息都有助于弄清楚。
  • 是的,我收到一条错误消息
  • 你能编辑你的问题并把它放在那里吗?

标签: php laravel-5


【解决方案1】:

Laravel Documentation One to many Relation

$dr->user->get()->all();

应该返回该医生的所有用户。

【讨论】:

    【解决方案2】:

    在查询中获取用户详细信息

    $doctors = Doctor::with('user')->get();
    

    在您的视图文件中

    <td>{{$dr->user->active_user}}</td>
    

    【讨论】:

    • 它返回错误消息“方法不存在。”
    • 应该是Doctor:with('user')-&gt;all();
    • 现在显示错误“在 Builder.php 第 2306 行调用未定义的方法 Illuminate\Database\Query\Builder::all()”
    猜你喜欢
    • 1970-01-01
    • 2019-05-26
    • 2018-03-08
    • 2020-01-15
    • 2019-02-15
    • 2021-12-21
    • 2015-11-29
    • 2018-08-06
    • 1970-01-01
    相关资源
    最近更新 更多