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

Rarepyre's avatar

How use eloquent hasManyThrough for join 3 tables?

I want to show (foreach) all row from Restaurant in my blade. but i'am so confused about this...

what i need like this (look at my syntax)

<table class="table table-responsive-md" id="table">
    <thead>
        <tr>
            <th scope="col">#</th>
            <th scope="col">Name</th>
            <th scope="col">Total Order</th>
            <th scope="col">Total Sold</th>
            <th scope="col">Total Commission</th>
            <th scope="col" colspan="2" class="text-center">Action</th>
        </tr>
    </thead>
    <tbody>
        @foreach($restaurantList as $key => $fr)
            <tr>
                <th scope="row">{{ $key+1 }}</th>
                <td>{{ $fr->name }}</td>
                <td>{{ count($fr->orders) }}</td>
                <td>
                    @if($fr->solds == '[]')
                        {{ formatted_price(0,'rupiah') }}
                    @else
                        @foreach($fr->solds as $fs)
                            {{ formatted_price($fs->amount + $fr->orders->sum('delivery_fee') - $fr->orders->sum('discount'),'rupiah') }}
                        @endforeach
                    @endif
                </td>
                <td>
                    @if($fr->commissions == '[]')
                        {{ formatted_price(0,'rupiah') }}
                    @else
                        @foreach($fr->commissions as $fc)
                            {{ formatted_price($fc->amount,'rupiah') }}
                        @endforeach
                    @endif
                </td>
                <td>
                    // i need to calculate like the (<td> above, but i need to add where clause (look at the model below)
                </td>
                <td class="text-center">
                    <a href="{{ route('nk.resellers.order-detail', $fr->id) }}" class="btn btn-success">Detail</a>
                </td>
                <td class="text-center">
                    <a href="{{ route('nk.resellers.disbursements.disburst', $fr->id) }}" class="btn btn-primary">Disburst</a>
                </td>
            </tr>
        @endforeach
    </tbody>
    </table>

here my tables:

restaurants (model: Restaurant)

-id

-name

TABLE orders (model: Order)

-id

-restaurant_id

-order_status_id

-name

TABLE order_statuses (model: OrderStatus)

-id

-key

restaurant_commissions (model: RestaurantCommission)

-id

-order_id

-commission_price

-is_disbursed

and i have added some relation for this model below:

Model Restaurant:

public function commissions()
{
    return $this->hasManyThrough(\App\Models\RestaurantCommission::class, \App\Models\Order::class)
        ->selectRaw('sum(commission_price) as amount')
       	->groupBy('orders.restaurant_id');
}    

public function disbursed_commissions()
{
    return $this->hasManyThrough(\App\Models\RestaurantCommission::class, \App\Models\Order::class)
        ->where('restaurant_commissions.is_disbursed',0)
        ->selectRaw('sum(commission_price) as amount')
       	->groupBy('orders.restaurant_id');

     // here, i need to join table orders to table order_statuses, then add the where clause like ->where('order_statuses.key','=','completed')
}

public function solds()
{
    return $this->hasManyThrough(\App\Models\FoodOrder::class, \App\Models\Order::class)
    ->selectRaw('sum(price) as amount')
    ->groupBy('orders.restaurant_id');
}

Model Order

public function orderStatus()
{
    return $this->belongsTo(\App\Models\OrderStatus::class, 'order_status_id', 'id');
}

public function restaurantCommission()
{
    return $this->hasOne(\App\Models\RestaurantCommission::class, 'order_id');
}

the example result should be :

No | Name | Total Order | Total Sold | Total Commission | Disbursed Commission | Action

1 | MyCafe | 4 | 39000 | 8000 | 3000 |detail/disburst

How to do it? thanks! your help is very needed

0 likes
1 reply
Rarepyre's avatar

done thank! i just put this syntax into Restaurant model

public function disbursed_commissions()
{
    return $this->hasManyDeep(\App\Models\RestaurantCommission::class, ['\App\Models\Order'])
        ->join('order_statuses', 'orders.order_status_id','=','order_statuses.id')
        ->where('order_statuses.key','completed')
        ->where('restaurant_commissions.is_disbursed',0)
        ->selectRaw('sum(commission_price) as amount')
       	->groupBy('orders.restaurant_id');
}

Please or to participate in this conversation.