I have collections of Books and Users. Each book has a user_id which is the ObjectId of a User. When a user is logged in I want to list all of their books. How could I find all documents in the books collection with a user_id equal to the currently logged in user?
Asked
Active
Viewed 194 times
1 Answers
0
use Illuminate\Support\Facades\Auth;
$user_id = Auth::user()->id;
$books = Book::where('user_id', $user_id)->get();
$user_id is the id of the user that is logged in.

Aashish gaba
- 1,726
- 1
- 5
- 14
-
Could you paste the exact error here?. What are you using for authentication? dd($user_id) to check what is the user id of the user that is logged in – Aashish gaba Jul 23 '20 at 17:04