Laravel Jetstream比较两个表来改变表的背景



我想做一个表,其中用户有一个组织,如果用户还没有组织的背景,该单元格会改变。我将数据存储在数据透视表中。我试着把数据透视表的user_id放在一个数组中,并在刀片中比较它,但它不起作用,如果我试着把它放入变量,我只得到最后一个值。唯一改变的背景是数组的最后一个值,而不是其他值。

这是刀片文件

@if($displayData->count())
@foreach($displayData as $item)
<tr>
<td class="px-6 py-4 text-sm whitespace-no-wrap">
{{ $item->id }}
</td>
<td class="px-6 py-4 text-sm whitespace-no-wrap">
{{ $item->name }}
</td>
<td class="px-6 py-4 text-sm whitespace-no-wrap">
{{ $item->email }}
</td>
<td class="px-6 py-4 text-sm whitespace-no-wrap">
{{ $item->created_at }}
</td>
<td class="px-6 py-4 text-sm whitespace-no-wrap">
{{ $item->updated_at }}
</td>
<td class="px-6 py-4 text-sm whitespace-no-wrap">
<x-jet-button wire:click="updateUserModel({{ $item->id }})">
{{__('Update User')}}
</x-jet-button>
<x-jet-button wire:click="updateUserPasswordModel({{ $item->id }})">
{{__('Update Password User')}}
</x-jet-button>
<x-jet-danger-button wire:click="deleteShowUserModal({{ $item->id }})">
{{__('Delete')}}
</x-jet-danger-button>
</td>
<td class="px-6 py-4 text-sm whitespace-no-wrap">
<x-jet-button wire:click="addShowRoleModel({{ $item->id }})">
{{__('Add Role')}}
</x-jet-button>
<x-jet-button wire:click="addShowTeamsModel({{ $item->id }})">
{{__('Create Team')}}
</x-jet-button>
</td>
<td class="px-6 py-4 text-sm whitespace-no-wrap" 
style="
@foreach($isUsersHaveAffliatedOrganization as $orgs)
@if($item->id == $orgs )
background: blue;
@else
background: red;
@endif
@endforeach
">
<x-jet-button wire:click="addShowOrganizationModel({{ $item->id }})">
{{__('Add Organization')}}
</x-jet-button>
</td>
</tr>
@endforeach
@else
<tr>
<td class="px-6 py-4 text-sm whitespace-no-wrap" colspan="4">
No Results Found
</td>
</tr>
@endif

这是控制器isUserHaveOrganization ()

public function isUserHaveOganization(){
$this->isUserOrgAuthId = DB::table('organization_user')->orderBy('id','asc')->get()->pluck('user_id');
return $this->isUserOrgAuthId;
}

read ()

public function read(){
return DB::table('users')->orderBy('id','asc')->paginate(10);
}

和渲染函数

public function render(){
return view('livewire.users',[
'displayData' => $this->read(),
'isUsersHaveAffliatedOrganization' => $this->isUserHaveOganization(),
]);
}

我不知道该怎么办。谢谢你

这里不需要foreach。您可以使用contains检查id是否在集合中,如下所示:

style="
@if($isUsersHaveAffliatedOrganization->contains($item->id))  
background: blue;
@else
background: red;
@endif
">

最新更新