0
votes

In form below my user share document access with other users. How to increase my selector to show me only user who are not shared with this document yet? Document and User has pivot table document_user

 <select type="text" name="user" class="uk-select">
        <option disabled selected>Choose from contacts</option>
        @foreach($company->users as $contact)
           //@if (something)
            <option value="{{ $contact->id }}">{{ $contact->first_name}} {{ $contact->last_name}}</option>
           @andif
        @endforeach
    </select>

Document = $document

Tables:

user:

  • id
  • name

document:

  • id;
  • company_id
  • name

document_user: - company_id - user_id

1
What is the relation between user and document? - iamab.in
public function users() { return $this->belongsToMany(User::class, 'document_user'); } - Philipp Kishkovarov
and user ` public function documents() { return $this->hasMany(Document::class, 'document_user'); }` - Philipp Kishkovarov
could you please add the table structure of the two tables to the question. I think you should have a many to many relation hasmany in user shoulld change to belongs to many - iamab.in
I added, check please - Philipp Kishkovarov

1 Answers

1
votes

Try this..

From your table structure I believe you should have the many to many relation.

User.php

public function documents()
{
    return $this->belongsToMany(Document::class, 'document_user');
}

xx.blade.php

<select type="text" name="user" class="uk-select">
    <option disabled selected>Choose from contacts</option>
    @foreach($company->users as $contact)
        @if(!in_array($contact->id, $document->users->pluck('id')->toArray()))
            <option value="{{ $contact->id }}">
                {{ $contact->first_name}} {{ $contact->last_name}} 
            </option>
        @endif
    @endforeach
</select>