Laravel Jetstream Comparing Two Tables to change the background of the table

  css, jetstream, laravel, laravel-livewire, tailwind-css

I am trying to make a table where a user have an organization and if the user doesnt have an organization yet the background for that cell will change. I am storing the data in a pivot table. I tried putting the user_id of the pivot table in an array and compare it in the blade but it doesn’t work, if I tried getting the putting it int variable i’m only getting the last value. The only background that changes is the last value of the array but not the rest.

This is the blade file

    @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

This is my controller
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);
}

and the render function

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

I’m not sure what to do. Thank you

Source: Laravel

Leave a Reply