'laravel livewire pre selected multiple checkbox get and update value

Basically, I am trying to make a multistep form like google forms and using livewire. By saving each single page data on next click. on some page, I am trying to get multiple checkbox data and save it to the database on click of next button, also if I want to get back to the same page I should get all selected checkboxes to be checked even if I refreshed the page.

Below is my livewire controller code.


public function mount()
    {
        $profileData = DB::table('investor_details')->where('user_id', Auth::user()->id)->first();
        $this->transaction_preferences = json_decode($profileData->transactional_preference,true) ?? DB::table('transaction_preference')->get()->toArray();
    }

Below is my render function in livewire controller

public function render()
    {
        return view('livewire.multi-step-form')->with('locations', $locations)->with('sectors', $sectors)->with('selected_sector');
    }

This is my save method i am calling this in another method

$this->validate([
                'transactional_preference'=>'required',
            ]);

            // foreach($this->transactional_preference as $temp){
            //     $transactionArray[] = $temp;
            //  }

            try
            {
                $date_time = Carbon::now()->toDateTimeString();
                DB::beginTransaction();

                DB::table('investor_details')
                ->updateOrInsert(
                    ['user_id' => Auth::user()->id],
                    ['transactional_preference' => $this->transactional_preference],
                );

                DB::commit();
            }
            catch (Exception $e)
            {
                DB::rollBack();
                session()->flash('message', 'Something went wrong while saving your public information.');
            }

Below is my livewire blade code.

@forelse ($transaction_preferences as $key => $transaction_preference)
    <li>
        <label class="checkbox-wrap">
            <input type="checkbox" value="{{ $transaction_preference['name'] }}" wire:model.defer="transactional_preference" >
            <span for="">{{ $transaction_preference['name'] }}</span>
            <span class="checkmark"></span>
        </label>
    </li>
@empty
    <li>
        <label class="checkbox-wrap">
            <input name="nodata" type="checkbox" value="">
            <span for="nodata">No Records</span>
            <span class="checkmark"></span>
        </label>
    </li>
@endforelse


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source