Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

akashbd's avatar

How to get three tables data in view page in Laravel 8

I have three different tables and different models. The tables are State Office, City Office, Hometown Office. I want to show the data of the office that the user will select between these three offices. There is a column in the user table called office_id. I'm brand new to Laravel so I don't know-how. Would you please tell me how to do this?

I have already add some code to my User Model

public function stateoffice()
    {
        return $this->hasOne(State::class, 'id', 'office_id');
    }

    public function cityoffice()
    {
        return $this->hasOne(CityOffice::class, 'id', 'office_id');
    }

    public function hometownoffice()
    {
        return $this->hasOne(HometownOffice::class, 'id', 'office_id');
    }

This is my controller code

public function AllUser(){
        $data['user'] = User::all();
        $data['stateoffice'] = StateOffice::all();
        $data['cityoffice'] = CityOffice::all();
        $data['hometownoffice'] = HometownOffice::all();
        return view('admin.all-user', $data);
    }

But I'm stuck in View how can I show it.

<thead>
    <th>Office Name</th>
</thead>
<tbody>
    @foreach($user as $data)
    <tr>
        <td>@if($data->office_id == NULL)
            <span>no data found</span>
            @else
            {{$data->office->office_name}}
            @endif
        </td>
    </tr>
0 likes
6 replies
KalimeroMK's avatar

try this

public function AllUser(){
       $users = User::with(['stateoffice','cityoffice','hometownoffice'])->get();
        return view('admin.all-user', compact('users));
    }

blade

<thead>
    <th>Office Name</th>
</thead>
<tbody>
    @foreach($users as $user)
    <tr>
        <td>@if($user->stateoffice->id == NULL)
            <span>no data found</span>
            @else
            {{$user->stateoffice->office_name}}
            @endif
        </td>
    </tr>
rodrigo.pedra's avatar

First, eager load User's relations:

public function AllUser()
{
    $data['user'] = User::with(['stateoffice', 'cityoffice', 'hometownoffice'])->get();
    return view('admin.all-user', $data);
}

Then, use the relations:

<thead>
    <th>Office Name</th>
</thead>
<tbody>
    @foreach($user as $data)
        <tr>
            <td>
                @if($data->stateoffice)
                    {{ $data->stateoffice->office_name }}
                @elseif($data->cityoffice)
                    {{ $data->cityoffice->office_name }}
                @elseif($data->hometownoffice)
                    {{ $data->hometownoffice->office_name }}
                @else
                    <span>no data found</span>
                @endif
            </td>
        </tr>
    @endforeach
</tbody>

Reference: https://laravel.com/docs/9.x/eloquent-relationships#eager-loading

1 like
akashbd's avatar

@rodrigo.pedra I worked the way you said but one problem is that it only shows the name of the state office but not the name of the city office and hometown office. Because in the user table there is only one office_id. What can be done now, sir?

rodrigo.pedra's avatar
Level 56

@akashbd you could:

1. Have a column for each office

<thead>
    <th>State Office</th>
    <th>City Office</th>
    <th>Hometown Office</th>
</thead>
<tbody>
    @foreach($user as $data)
        <tr>
            <td>
                @if($data->stateoffice)
                    {{ $data->stateoffice->office_name }}
                @else
                    <span>no data found</span>
                @endif
            </td>
            <td>
                @if($data->cityoffice)
                    {{ $data->cityoffice->office_name }}
                @else
                    <span>no data found</span>
                @endif
            </td>
            <td>
                @if($data->hometownoffice)
                    {{ $data->hometownoffice->office_name }}
                @else
                    <span>no data found</span>
                @endif
            </td>
        </tr>
    @endforeach
</tbody>

2. Display all offices in the same cell:

<thead>
    <th>Office Name</th>
</thead>
<tbody>
    @foreach($user as $data)
        <tr>
            <td>
                @if($offices = collect([
                    $data->stateoffice,
                    $data->cityoffice,
                    $data->hometownoffice
                ])->filter()->isNotEmpty())
                    {{ $offices->pluck('name')->join(',', ', and ') }}
                @else
                    <span>no data found</span>
                @endif
            </td>
        </tr>
    @endforeach
</tbody>

References:

1 like

Please or to participate in this conversation.