zaster's avatar

Livewire - I only can Add records to the pivot table - Through checkboxes

I am only able to add records to the pivot table. Really strugling on removing an entry from the pivot table

Livewire Component

<?php

namespace App\Http\Livewire;

use App\Models\User;
use App\Models\Company;
use Livewire\Component;
use Livewire\WithPagination;

class UserAssignCompanySection extends Component
{
    use WithPagination;

    public $sortBy = 'id';
    public $sortAsc = false;
    public $user;
    public $companies;
    public $search;
    public $confirmingCompanyRemoval = false;
    public $userCompanies = [];

    protected $rules = [
        'user.name' => 'required',
        'user.email' => 'required',
        'user.title' => 'required',
        'user.first_name' => 'required',
        'user.last_name' => 'nullable',
        'user.mobile' => 'nullable',
        'user.phone' => 'nullable',
        'user.is_customer' => 'boolean',
        'user.is_provider' => 'boolean',
        'userCompanies' => 'required|array',
    ];

    // Table Sort
    public function sortBy($field)
    {
        if($field == $this->sortBy){
            $this->sortAsc = !$this->sortAsc;
        }
         $this->sortBy = $field;
    }


    // public function updatedUserCompanies()
    // {

    //     $var1 = array_diff(Company::pluck('id')->toArray(),$this->userCompanies);

    //     dd(Company::pluck('id')->toArray(), $this->userCompanies);
    //     dd($var1);
    //     $this->user->companies()->sync($this->userCompanies);
    // }

    public function updatedUserCompanies()
    {
        //dd($this->userCompanies);
        $this->user->companies()->sync($this->userCompanies);

    }

    public function mount(User $user)
    {
        $this->user = $user;
        $this->companies = Company::all();
        $this->userCompanies = $user->companies()->pluck('company_id')->toArray();
        //dd($this->userCompanies);
    }

    public function render()
    {
        //$this->user->companies()->sync($this->userCompanies);
        return view('livewire.user-assign-company-section');
    }
}


<div>
    <div class="grid grid-cols-1 text-left xl:grid-cols-6">
        <div class="col-span-3 p-4">
            <p class="text-lg underline">Name</p>
            <x-jet-input wire:model.defer="user.name" id="name" type="text" disabled class="block w-full mt-1 bg-gray-100" />
            <x-jet-input-error for="user.name" class="mt-2" />
            <br>

            @json($userCompanies);


            {{-- @foreach ($userCompanies as $item)
                {{ $item }}
            @endforeach --}}

            @foreach($companies as $company)
            <div class="mt-1">
                <input wire:model="userCompanies" type="checkbox" value="{{ $company->id }}">
                    {{ $company->name }} <br>
            </div>
            @endforeach
            <br>
            {{-- <a href="{{ url()->previous() }}"><x-jet-button>Back</x-jet-button></a> --}}
            <br>
        </div>
    </div>
</div>

When i click on the checbox with company_id = 3 a sting gets added to $this->userCompanies livewire property

something like this

[1,2,"3"];

When i uncheck ids such as 1 and 2 (which was retreived through the DB won't get deleted)

I think it tries to delete a string such as "1" from an array based on integers

0 likes
6 replies
Snapey's avatar

what does dd show when you remove a company

zaster's avatar

@snapey

id: 28

user_id: 12

company_id: 1

id: 29

user_id: 12

company_id: 2

http://127.0.0.1:8000/employee/other/users/assigns/12/edit

@json($userCompanies); outputs this

[1,2];

When I uncheck $company->id = 1

    public function updatedUserCompanies()
    {
        dd($this->userCompanies);
        $this->user->companies()->sync($this->userCompanies);

    }

outputs

array:2 [▼
  0 => 1
  1 => 2
]

Nothing is detected

When i select the same checkbox it outputs

array:3 [▼
  0 => 1
  1 => 2
  2 => "1"
]
zaster's avatar

@snapey

I think i am almost there. What i want is to get the keys of the below array (the keys which has the value 1) and sync them with the pivot table

array:3 [▼
  1 => 1
  2 => 1
  3 => false
]

Now my code looks like this

public function updatedUserCompanies()
    {
        dd($this->userCompanies);
        // dd(array_keys($this->userCompanies));
        $this->user->companies()->sync($this->userCompanies);
    }

    public function mount()
    {

        $this->companies = Company::all();

        $this->userCompanies = array_fill_keys($this->user->companies->pluck('id')->toArray(), 1);

    }
<div>
    <div class="grid grid-cols-1 text-left xl:grid-cols-6">
        <div class="col-span-3 p-4">
            <p class="text-lg underline">Name</p>
            <x-jet-input wire:model.defer="user.name" id="name" type="text" disabled class="block w-full mt-1 bg-gray-100" />
            <x-jet-input-error for="user.name" class="mt-2" />
            <br>

            @json($userCompanies)

            <br><br>
            @foreach($companies as $company)
                    <input type="checkbox" value="1"
                               wire:model="userCompanies.{{ $company->id }}">
                        {{ $company->name }}
                @endforeach
            {{-- <a href="{{ url()->previous() }}"><x-jet-button>Back</x-jet-button></a> --}}
            <br>
        </div>
    </div>
</div>
Snapey's avatar

You need to get the array_keys where the value is not false.

If you make it a collection, you can use

->filter()->keys();

like

$this->user->companies()->sync(collect($this->userCompanies)->filter()->keys());

2 likes
zaster's avatar

@snapey Thank you very much. Had to struggle alot to get this done.

Please or to participate in this conversation.