带透视表的表单查询

cnh2zyt3  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(229)

在下面的表单中,我的用户与其他用户共享文档访问权限。如何增加我的选择器以仅显示尚未与此文档共享的用户? Document 以及 User 有数据透视表 document_user ```

Choose from contacts
@foreach($company->users as $contact)
//@if (something)
{{ $contact->first_name}} {{ $contact->last_name}}
@andif
@endforeach

文档=$document
table:
用户:
身份证件
名称
文件:
身份证件;
公司id
名称
文档用户:-公司id-用户id
zqdjd7g9

zqdjd7g91#

试试这个。。
从你的table结构来看,我认为你应该 many to many 关系。
用户.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>

相关问题