php - 我需要在 laravel 中按每个用户表显示数据
问题描述
如何为每个用户显示数据这是我的代码
模型
# User Model
public function client()
{
return $this->belongsTo('App\Http\Models\Client');
}
# Client data Model
public function user()
{
return $this->belongsTo('App\Http\Models\User');
}
# Controller
public function porteil()
{
$user_id = auth()->user()->id;
$client = User::find($user_id);
return view ('client.show', $this->data)->with('clients', $client)
->with('progresing', Progress::all());
}
看法
@foreach($clients as $client)
<tr class="text-center">
<th class="text-center"><input type="checkbox"></th>
<td class="nowrap">{{ $client->n_dossier }}</td>
<td class="nowrap">{{ $client->user['name'] }}</td>
<td class="nowrap">{{ $client->progress['name'] }}</td>
<td class="text-center">
<span class="badge badge-yellow">Télécharger</span>
</td>
</tr>
@endforeach
解决方案
您可以使用以下方法急切地加载您的关系with
:
public function porteil()
{
$clients = Client::with(['user', 'progress'])->get();
return view('client.show', $this->data)
->with('clients', $clients)
->with('progresing', Progress::all());
}
在你的刀片上:
@foreach($clients as $client)
<tr class="text-center">
<th class="text-center"><input type="checkbox"></th>
<td class="nowrap">{{$client->n_dossier}}</td>
<td class="nowrap">{{$client->user->name}}</td>
<td class="nowrap">{{$client->progress->name}}</td>
<td class="text-center">
<span class="badge badge-yellow">Télécharger</span>
</td>
</tr>
@endforeach