spoon
4 years ago

Eloquent where

Posted 4 years ago by spoon

Hi!

I want to get the all books that has been read by a user, but I kinda stuck here.

My usersxbook_progress table has three columns and they are foreign keys. user_id, progress_id and thing_id. progress_id = 1 means that book is completed, but how do I list only completed books by a user?

User Model

    public function usersBooks1() {
        return $this->belongsToMany('App\Models\Thing', 'usersxthing_progress','user_id','thing_id');
    }

Controller

        $allbooks = User::find($user_id)->usersBooks1;

View

@foreach($allbooks as $books)
    {{$books->name}}
@endforeach

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