SarahS74
2 weeks ago
28
15
Laravel

Getting extra data from pivot table

Posted 2 weeks ago by SarahS74

I have an Users table and a Books table. I also have a Book_User pivot table with 3 extra columns (own|read|wishlist).

I'm trying to show on my Author page a list of Books the author has and then via Buttons which ones I have read/owned/wishlist.

            @foreach ($author->books->sortByDesc('year') as $book)
            <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 {{  $book->pivot->own === "1" ? "btn-primary" : "btn-secondary" }}" name="read" value="1">Read It</button>
                        <button class="btn btn-sm btn-primary" name="own" value="1">Own It</button>
                        <button class="btn btn-sm btn-primary" name="wish" value="1">Wishlist</button>
                    </form>
                </td>
            </tr>
            @endforeach

I can't seem to get the data from the pivot table though to work. This line here:

 $book->pivot->own

doesn't do it. Where am I going wrong?

Please sign in or create an account to participate in this conversation.