Reputation: 495
I'm new to laravel and I can't seem to display a data from another table that is not related. I have three tables, Department, Section and Position.
Department Has Many Section
Section Has Many Position
I want to display the department name from the Department table in the position view.
Position Controller
$getPositionWithSection = PositionModel::with('section')->get();
return view('PositionView.add', compact('getPositionWithSection'));
Position View HTML
@foreach($getPositionWithSection as $id => $employee)
<tr>
<td>{{ ++$id }}</td>
<td>{{ $employee->section->department_name }}</td>
<td>{{ $employee->section->section_name }}</td>
<td>{{ $employee->position_name }}</td>
</tr>
@endforeach
I was able to display the name of the section, but how do I display the name of the department?
Upvotes: 1
Views: 399
Reputation: 1373
@foreach($getPositionWithSection as $id => $employee)
<tr>
<td>{{ ++$id }}</td>
<td>{{ $employee->section->department->department_name}}</td>
<td>{{ $employee->section->section_name }}</td>
<td>{{ $employee->position_name }}</td>
</tr>
@endforeach
Upvotes: 1