首页 > 解决方案 > 如何通过 2 表 FK 在刀片上显示数据

问题描述

我有 3 个表,例如usersdata_userspractice

users      : id , name ,dob ,email , etc . . .

data_users : id , user_id , number_exp , etc . .

practice   : id , data_user_id , practice_number , etc ....

在 3 个表中,通过关系 belongsTo 连接并且想要显示这个。我想显示有关表练习的数据:

我的控制器:

public function show()
{
    $practice= Practice::with('data_users')->get();

    return view('admin.practice' ,['practice'=>$practice]);
}

我的观点

 <table class="table table-bordered">
              <thead>
                <tr>
                  <th>No</th>
                  <th>Name</th>
                  <th>No Surat</th>
                  <th>Date</th>
                  <th>Status</th>
                </tr>
              </thead>
              <tbody>
                @foreach ($practice as $i)
                <tr>
                  <th scope="row">1</th>
                  <td>{{ $i->data_users->users->name}}</td>//i want to show name but i cant show this
                  <td>{{ $i->practice_number}}</td>
                  <td>{{ $i->practice_date}}</td>
                  <td>{{ $i->status}}</td>
                </tr>
                @endforeach
              </tbody>
            </table>

我在显示数据“名称”表格用户时遇到问题,我只能 从data_users显示id

但仍然错误尝试获取财产

标签: laravel

解决方案


您正在$practice从控制器返回集合,试图 foreach$riwayat集合。


推荐阅读