I have a collection like the below. I need to fetch those users, whom does not uploaded documents. Now from the relationship, How can I fetch this?
[0] => Array ( [id] => 2 [user_id] => 8 [referred_by] => ........ [user_documents] => ) [1] => Array ( [id] => 3 [user_id] => 9 [referred_by] => [reference_id] => NM1100008 ....... [user_documents] => Array ( [id] => 1 [customer_id] => 3 [doc_type] => 1 <div class="su-document su-u-responsive-media-yes"></div> => N8xFORsPpkTayTQ9Ihyz0ly7QM62TJHxvHhSFQSN.png [admin_comment] => [approve_status] => approve [other_gov_id] => [doc_no] => 1 [created_at] => 2020-04-04 12:39:12 [updated_at] => 2020-04-06 13:57:01 [deleted_at] => ) )
I want to get rows only when user_documents is empty.. To get those users who does not uploaded documents to the system.
[user_documents] =>
Thanks in advance.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can use laravel querying-relationship-absence
$users= User::doesntHave('user_documents')->get();
Method 2
Depending on how you have setup your relations, something like this should work:
User::doesntHave('documents')->get();
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0