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

SarahS's avatar
Level 12

In-depth pivot table tutorials

I'm really struggling to do something that seems simple in PHP but I can't do in Laravel. It involves pivot tables and I can't find any useful documentation or tutorials that might help.

So, I'm not sure if this should be in 'Requests' or 'Feedback' but something on pivot tables would be good.

0 likes
6 replies
SarahS's avatar
Level 12

I've done that already. It doesn't cover what I need.

janosk's avatar

Hi @sarahs74 ! The video series skauk mentioned is quite good, but of course it is possible it does not cover a few aspects of that relationship.

Beside the videos, there is the Laravel docs: https://laravel.com/docs/6.0/eloquent-relationships#updating-many-to-many-relationships

and the API docs: https://laravel.com/api/6.x/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.html

If none of these three sources covers the functionality you try to achieve, it might not be as simple as it looks at first glance. In that case, please be a little more specific, so we can try to suggest more appropriate solutions.

SarahS's avatar
Level 12

I thought I'd solved the problem, I have this and it does work but it only shows books that the current user has a relationship with. I need it to show all the books an author has written.

              @foreach ($author->books as $book) 
          @foreach ($book->users as $bookuser)
            <tr>
                <td>
                    <a href="/books/{{ $book->id }}">{{ $book->title }}-<em>{{ $book->series->name }} Series</em> ({{ $book->year }})</a>
                </td>
                <td>
                    <form action="{{ $author->path() . '/books/'.$book->id }}" method="POST">
                        @csrf 
                        <button class="btn btn-sm {{  $bookuser->id != $user->id ? "" : ($bookuser->library->read === 1 ? "btn-primary" : "btn-secondary") }}" name="read" value="1">Read It</button>
                        <button class="btn btn-sm {{  $bookuser->id != $user->id ? "" : ($bookuser->library->own === 1 ? "btn-primary" : "btn-secondary") }}" name="own" value="1">Own It</button>
                        <button class="btn btn-sm {{  $bookuser->id != $user->id ? "" : ($bookuser->library->wishlist === 1 ? "btn-primary" : "btn-secondary") }}" name="wish" value="1">Wishlist</button>
                    </form>
                </td>
            </tr>
              @endforeach
            @endforeach
imawesome's avatar

Maybe considering to create a new thread? For me seems a little dim. To show all the books of an author I think it would be enough:

@foreach ($author->books as $book) 
    process here each book
@endforeach

Maybe do you wanna show all the books who got $author and current user? Something like this?

@foreach ($user->author->books as $book) 
    process here each book
@endforeach
SarahS's avatar
Level 12

I've already got a few threads on this because it is proving impossible.

It's a page with details of the Author and all their books. I want to see which books the current User has etc. on that page as well.

Please or to participate in this conversation.