in my Laravel
app I have 3 tables : users
, documents
and type_documents
, the user have multiple documents and document have one type_document
| Documents |
| -------- |
| id |
| file |
| type_document_id|
| user_id |
| type_documents |
| -------- |
| id |
| name |
| users |
| -------- |
| id |
| name |
| type_document_d|
I want select the types that are not used in documents
table for the current user with eloquent
I try with this, but it give me the used type_documents :
$document_types = TypeDocument::leftJoin('docments' , function ($join) {
$join->on('type_documents.id', '=', 'douments.type_document_id')
->where('documents.user_id', auth()->id());
})
->applyFilters($request->all())
->latest()
->paginateData($limit);
I use Laravel version 8
3
Answers
Check this solution
Have you tried whereNotExists() or whereNotIn() instead of leftJoin()?
eg:
try any of these!
Using Eloquent relation defined between
TypeDocument::class
andUser::class
like this:TypeDocument.php
The you can easily get the type document not linked to a specific user like this
————–EDIT————–
IF you want to use two step relation, you can define it like this:
TypeDocument.php
Document.php
And use the relations to query it like this: