Document::query()
->whereRelation('categories', fn ($builder) => $builder->where( 'category_id', 5))
->whereRelation('categories', fn ($builder) => $builder->where( 'category_id', 10))
->get();
In a single query, all where clauses need to be matched by each record. Therefore, you won't have a record with two different values for a single column. That is why using two different values to test the same columns doesn't work.
The query above will test each individual value separately, and as they are both where clauses to the same parent query, only records matching both separate where clauses will be fetched.