Jim E Russel
Jim E Russel

Reputation: 495

Laravel - Get data from another table (no relationship) and display on view

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

Answers (1)

Ahmad Karimi
Ahmad Karimi

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

Related Questions