Reputation: 27
i Have 3 Table , the name is : user , machine , maintance . this schema like this
*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 ..
now i want to show data on table "Maintance" with show data "name" (user table) and name_machine( machine table) by id .
this is my controller and my view
public function show($id)
{
$maintance = Maintance::all();
return view('users.view',['maintance' => $maintance]);
}
my view
<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>
this view data is not having error ,but i dont know how to showing this data from different table . can someone help ?
Upvotes: 1
Views: 97
Reputation: 1092
Assuming your Maintance
model belows,
public function user(){
return $this->belongsTo(User::class);
}
public function machine(){
return $this->belongsTo(Machine::class);
}
assuming that your Model have these above relations.
and do the below to get your data,
$maintance = Maintance::with(['user','machine'])->get();
and in your view
<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>
Upvotes: 1