如何通过 laravel 中的 id select 不同 table 上的关系数据
How to select relational data on different table by id in laravel
我有3个Table,名字分别是:user,machine,maintance。这个架构像这样
*User
id | name | email | password | created_at | updated_at
1 | john | A@mail | ******* , etc
*Machine
id | name_machine | created_at | updated_at
1 | shock | .....etc
*Maintance
id | Machine_id | user_id | question | status | created_at |
updated_at
1 | .........1........ |.......1......| blablabla etc ..
现在我想在 table "Maintance" 上显示数据 "name"(用户 table)和 name_machine(机器 table) 通过 id .
这是我的控制器和视图
public function show($id)
{
$maintance = Maintance::all();
return view('users.view',['maintance' => $maintance]);
}
我的观点
<table class="table table-hover">
<tbody><tr>
<th>Machine</th>
<th>Question</th>
<th>User Input</th>
<th>Action</th>
<th>Tanggal</th>
</tr>
@foreach ($maintance as $i)
<tr>
<td>{{ $i->machine_id}} </td>// i want to show name machine here
<td>{{ $i->Question}}</td>
<td>{{ $i->user_id}}</td> // name users to
<td><span class="label label-primary">Lihat Data</span></td>
<td>{{ $i->created_at}}</td>
</tr>
@endforeach
</tbody></table>
此视图数据没有错误,但我不知道如何从不同 table 显示此数据。有人可以帮忙吗?
假设您的 Maintance
型号如下,
public function user(){
return $this->belongsTo(User::class);
}
public function machine(){
return $this->belongsTo(Machine::class);
}
假设您的模型具有以上这些关系。
并执行以下操作以获取您的数据,
$maintance = Maintance::with(['user','machine'])->get();
在您看来
<table class="table table-hover">
<tbody><tr>
<th>Machine</th>
<th>Question</th>
<th>User Input</th>
<th>Action</th>
<th>Tanggal</th>
</tr>
@foreach ($maintance as $i)
<tr>
<td>{{ $i->machine->machine_name}} </td>// i want to show name machine here
<td>{{ $i->Question}}</td>
<td>{{ $i->user->name}}</td> // name users to
<td><span class="label label-primary">Lihat Data</span></td>
<td>{{ $i->created_at}}</td>
</tr>
@endforeach
</tbody></table>
我有3个Table,名字分别是:user,machine,maintance。这个架构像这样
*User
id | name | email | password | created_at | updated_at
1 | john | A@mail | ******* , etc
*Machine
id | name_machine | created_at | updated_at
1 | shock | .....etc
*Maintance
id | Machine_id | user_id | question | status | created_at | updated_at
1 | .........1........ |.......1......| blablabla etc ..
现在我想在 table "Maintance" 上显示数据 "name"(用户 table)和 name_machine(机器 table) 通过 id .
这是我的控制器和视图
public function show($id)
{
$maintance = Maintance::all();
return view('users.view',['maintance' => $maintance]);
}
我的观点
<table class="table table-hover">
<tbody><tr>
<th>Machine</th>
<th>Question</th>
<th>User Input</th>
<th>Action</th>
<th>Tanggal</th>
</tr>
@foreach ($maintance as $i)
<tr>
<td>{{ $i->machine_id}} </td>// i want to show name machine here
<td>{{ $i->Question}}</td>
<td>{{ $i->user_id}}</td> // name users to
<td><span class="label label-primary">Lihat Data</span></td>
<td>{{ $i->created_at}}</td>
</tr>
@endforeach
</tbody></table>
此视图数据没有错误,但我不知道如何从不同 table 显示此数据。有人可以帮忙吗?
假设您的 Maintance
型号如下,
public function user(){
return $this->belongsTo(User::class);
}
public function machine(){
return $this->belongsTo(Machine::class);
}
假设您的模型具有以上这些关系。
并执行以下操作以获取您的数据,
$maintance = Maintance::with(['user','machine'])->get();
在您看来
<table class="table table-hover">
<tbody><tr>
<th>Machine</th>
<th>Question</th>
<th>User Input</th>
<th>Action</th>
<th>Tanggal</th>
</tr>
@foreach ($maintance as $i)
<tr>
<td>{{ $i->machine->machine_name}} </td>// i want to show name machine here
<td>{{ $i->Question}}</td>
<td>{{ $i->user->name}}</td> // name users to
<td><span class="label label-primary">Lihat Data</span></td>
<td>{{ $i->created_at}}</td>
</tr>
@endforeach
</tbody></table>