Laravel Eloquent:如何用名称替换id

时间:2017-11-27 08:53:27

标签: php laravel eloquent laravel-eloquent laravel-controller

我有以下型号和控制器。

user.php的

public function reporting() 
{
    return $this->hasMany('App\Reporting','user_id','id');
}

RegisterController.php

public function viewUsers()
{

    $users = User::with('reporting')->get();

    return view('users',compact('users'));
 }

users.blade.php

@foreach($users as $user)
        <tr>
            <td>{{$user->name}}</td>
            <td>
                @foreach($user->reporting as $report)
                {{$report->reporting}}
                @endforeach
            </td>
        </tr>
    @endforeach

上面的代码重新编写

name                                  Reporting Person
========================================================
Ankur /*gets from user table*/          35 //gets from reporting table

报告人的姓名是Yadav,此名称在用户表中如何替换报告人员ID 35以在视图中命名Yadav

0 个答案:

没有答案
相关问题