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

HarryN's avatar

Limiting nested relationships

I have these three models User.php Booking.php Company.php and the relationships are as follows.

User.php

    public function companies()
    {
        return $this->belongsToMany('App\Company');
    }

Company.php

    public function owners()
    {
        return $this->belongsToMany('App\User');
    }

    public function bookings()
    {
        return $this->hasMany('App\Booking');
    }

Booking.php

    public function company()
    {
        return $this->belongsTo('App\Company');
    }

On my page I pass this from my controller into my blade file.

        $myCompanies = User::with('companies')->find(Auth::user()->id);
        return view('home', compact('myCompanies'));

Then in my view I foreach through the companies and then inside that I loop through each companies bookings

[Sidenote: I'm not too sure how this is working but I am guessing when I use $company->bookings it finds the data from the Model's relationship in Company.php? Would love to know how this works.]

<div class="home-main">
    @foreach ($myCompanies->companies as $company)
        <div class="home-company">
            <h2><a href="{{ URL::to('company/' . $company->slug) }}">{{ $company->name }} Bookings</a></h2>
            <ul class="booking-list">
                @if (count($company->bookings))
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>Adults</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($company->bookings AS $booking)
                            <tr>
                                <td>{{ $booking->first_name }}</td>
                                <td>{{ $booking->adults }} Adults</td>
                                <td><a href="{{ URL::to('company/'.$company->slug.'/'.$booking->id.'') }}" class="btn btn-primary pull-right">VIEW BOOKING</a></td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
                @else
                <li>This company has no future bookings.</li>
                @endif
            </ul>
        </div>
    @endforeach
</div>

Now this all works fine. But as this is on the homepage I want it show an overview of each companies bookings. Therefore I'd like to limit the amount of bookings shown to x amount (lets say 10 for an example)

I've spent a while trying to research this and although I could just do a counter in the foreach I'd like to see if this is possible with laravel/eloquent.

I have tried this in the controller but it doesn't work.

$myCompanies = User::with('companies')->find(Auth::user()->id)->take(10);

0 likes
2 replies

Please or to participate in this conversation.