Bilal
Bilal

Reputation: 13

setting livewire switch default value to ON

I have a form which shows a customer phone number and implemented with livewire. We can add multiple phone numbers in the form. There is a switch beside the phone named primary if its active the phone number is displayed while listing customers. By default the primary switch is OFF how can I change it's default behaviour and make it ON. The blade file code for switch is as follows.

<div class="col-sm-1">
    <div class="form-group" wire:ignore>
        <label>{{ ucfirst(__('laravel-crm::lang.primary')) }}</label>
        <input type="checkbox" wire:model="primary.{{ $value }}" name="phones[{{ $value }}]       [primary]" data-toggle="toggle" data-size="sm" data-on="Yes" data-off="No" data-onstyle="success" data-offstyle="danger">
        @error('primary.'.$value) <span class="text-danger invalid-feedback-custom">{{ $message }}</span>@enderror
    </div>
</div>
<?php

namespace VentureDrake\LaravelCrm\Http\Livewire;

use Livewire\Component;

class LivePhoneEdit extends Component
{
    public $phones;
    public $number;
    public $type;
    public $primary;
    public $phoneId;
    public $old;
    public $updateMode = false;
    public $inputs = [];
    public $i = 0;

    public function mount($phones, $old)
    {

        // $validData = $request->validate([
        //     'phone' => ['required', 'numeric', 'digits:10'],
        // ]);

        $this->phones = $phones;
        $this->old = $old;

        if ($this->old) {
            foreach ($this->old as $phone) {
                $this->add($this->i);
                $this->number[$this->i] = $phone['number'] ?? null;
                $this->type[$this->i] = $phone['type'] ?? null;
                $this->primary[$this->i] = $phone['primary'] ?? null;
                $this->phoneId[$this->i] = $phone['id'] ?? null;
            }
        } elseif ($this->phones && $this->phones->count() > 0) {
            foreach ($this->phones as $phone) {
                $this->add($this->i);
                $this->number[$this->i] = $phone->number;
                $this->type[$this->i] = $phone->type;
                $this->primary[$this->i] = $phone->primary;
                $this->phoneId[$this->i] = $phone->id;
            }
        } else {
            $this->add($this->i);
        }
    }

    public function add($i)
    {
        $i = $i + 1;
        $this->i = $i;
        array_push($this->inputs, $i);
        $this->dispatchBrowserEvent('addPhoneInputs');
    }

    public function remove($i)
    {
        unset($this->inputs[$i]);
    }

    public function render()
    {
        return view('laravel-crm::livewire.phone-edit');
    }

    private function resetInputFields()
    {
        $this->number = '';
        $this->type = '';
        $this->primary = 0;
        $this->phoneId = '';
    }
}

I tried to set the primary value to 1 in all the above fields one by one and also set the default value in database to 1 but its not working because of the dynamic nature I am not able to find out where I should look or set the default value for my switch.

Upvotes: 0

Views: 42

Answers (0)

Related Questions