mhmiton / laravel-modules-livewire

Using Laravel Livewire in Laravel Modules package with automatically registered livewire components for every modules.
MIT License
187 stars 34 forks source link

Dependent Dropdown Value not loaded when using Laravel Modules Livewire #24

Closed price2b closed 2 years ago

price2b commented 2 years ago

Hi, I have a question: Im working with a dependent dropdown component. Do you know if Laravel-modules-livewire changes the way it displays selected previous values?

My current code:

`<?php

namespace Modules\Shipper\Http\Livewire;

use Livewire\Component; use Modules\Shipper\Entities\City; use Modules\Shipper\Entities\Country; use Modules\Shipper\Entities\State;

class CSC extends Component { public $countries; public $states; public $cities;

public $selectedCountry = null;
public $selectedState = null;
public $selectedCity = null;

public function mount($selectedCity = null)
{
    $this->countries = Country::all();
    $this->states = collect();
    $this->cities = collect();
    $this->selectedCity = $selectedCity;

    if (!is_null($selectedCity)) {
        $city = City::with('state.country')->find($selectedCity);
        if ($city) {
            $this->cities = City::where('state_id', $city->state_id)->get();
            $this->states = State::where('country_id', $city->state->country_id)->get();
            $this->selectedCountry = $city->state->country_id;
            $this->selectedState = $city->state_id;
        }
    }
}

public function render()
{
    return view('shipper::livewire.c-s-c');
}

public function updatedSelectedCountry($country)
{
    $this->states = State::where('country_code', $country)->orderBy('name', 'asc')->get();
    $this->selectedState = NULL;
}

public function updatedSelectedState($state)
{
    if (!is_null($state)) {
        $this->cities = City::where('state_id', $state)->orderBy('name', 'asc')->get();
    }
}

}`

my view:

<div class="col-md-9">
    <div class="form-group"><label for="country">{{ trans('ship.Shipper Country') }}<span>*</span></label>
        <select wire:model="selectedCountry" class="form-control" name="ship_country" id="ship_country" required>
            <option value="" selected>{{ trans('ship.Shipper Country') }}</option>
            @foreach($countries as $country)
                <option value="{{ $country->iso_code }}">{{ $country->country }}</option>
            @endforeach
        </select>
    </div>
</div>

@if (!is_null($selectedCountry))
    <div class="col-md-9">
        <div class="form-group"><label for="state">{{ trans('ship.Shipper State') }}<span>*</span></label>
            <select wire:model="selectedState" class="form-control" name="ship_state" id="ship_state" required>
                <option value="" selected>{{ trans('ship.Shipper State') }}</option>
                @foreach($states as $state)
                    <option value="{{ $state->iso2 }}">{{ $state->name }}</option>
                @endforeach
            </select>
        </div>
    </div>
@endif

Im surprised because debugging livewire it displays all the dependent dropdown values (states) correctly but its not displaying the dependent state dropdown in laravel view. When the livewire works (not using modules/livewire) when country is selected displays: selectedCountry="AR" selectedState=0

Now displays: selectedCountry="AR" selectedState=Null.

Screenshot 2022-06-14 at 7 15 34 PM

Any guidedance appreciated!.

price2b commented 2 years ago

weird.

mhmiton commented 2 years ago

@price2b I am extremely sorry for the late response. For my exam, I did not available. I tried to fix this issue, but it was also hard for me to a proper understanding. Is possible to share your demo project or maybe it will be better if I connect with your pc and try to solve this issue. Can we add on discord.com, please? This is my discord id - mhmiton#8254.

price2b commented 2 years ago

thank you. added using my account discord siherrera#7542

price2b commented 2 years ago

Just a comment and thats why I opened the case. This example is based on https://www.youtube.com/watch?v=pfSjRcudZVA Livewire: 3-Level Dependent Dropdowns by LaravelDaily with repository at -> https://github.com/LaravelDaily/Livewire-jQuery-Three-Dropdowns

the weird thing is that it works perfect in a vanilla laravel installation and it can be installed using Laravel Modules livewire. The component renders ok but it is not passing selectedState or selectedCity despite it shows on laravel debugbar (livewire) that state changed and charges all States the values from DB. (you can see the dependent states from the selected country).

But selectedCountry = "correctly the country" selectedState = "null" selectedCity = "null"

when it works in a vanilla installation it displays: selectedCountry = "correctly the country" selectedState = "null" selectedCity = 0

price2b commented 2 years ago

Thank you so much for your time and help using teamwiewer. my fault. the answer:

It was a <div></div> in the beginning of each component I didnt know it was mandatory to place!.

thank you!