首页 > 解决方案 > Livewire - 依赖下拉列表

问题描述

在编辑模式下,用户显示在选择下拉列表中,但未显示用户的公司(显示空白公司)然后我需要选择另一个用户并再次返回相关用户以制作公司列表出现。

Livewire 组件看起来像这样

<?php

namespace App\Http\Livewire;

use Carbon\Carbon;
use App\Models\Comp;
use App\Models\User;
use Livewire\Component;
use App\Models\Quotation;

class QuotationForm extends Component
{
    public Quotation $quotation;

    public $users; public $user;
    public $companies; public $company;
    public $comps; public $comp;

    protected function rules()
    {
        return [
            'quotation.user_id' => 'required',
            'quotation.company_id' => 'required',
            'quotation.comp_id' => 'required',
            'quotation.number' => 'required',
            'quotation.date' => 'required',
            'quotation.title' => 'required',
            'quotation.to' => 'required',
            'quotation.address' => 'nullable',
            'quotation.description' => 'nullable',
            'quotation.delivery' => 'nullable',
            'quotation.validity' => 'nullable',
            'quotation.costing' => 'nullable',
        ];
    }

    //For real-time validation
    public function updated($propertyName)
    {
        $this->validateOnly($propertyName);
    }

    public function updatingUser($user_id)
    {

        //get this users companies
        $user = User::findOrFail($user_id);

        if($user->count() > 0)
        {
            $this->companies = $user->companies;
        }

        $this->quotation->to = $user->name;

    }

    public function updatedUser()
    {
        $this->company ?? $this->companies->first()->id;
    }

    public function updatedCompany($company_id)
    {
        $this->quotation->address = 'test';
    }

    public function mount(Quotation $quotation)
    {
        //dd($quotation->user_id);
        $this->quotation = $quotation ?? new Quotation();

        $this->quotation->date = Carbon::now()->format('Y-m-d');

        //Dependant Dropdown - Customer Company
        $this->users = User::where('is_customer', 1)->orderBy('name', 'ASC')->get();
        $this->companies = collect();


        $this->comps = Comp::all();
        $this->comp = "1";

        //Dependant Dropdown - Customer Company in Edit Mode
        if($quotation->count() > 1)
        {
            $this->user = $this->quotation->user_id;
            $this->company = $this->quotation->company_id;
        }
        $this->quotation->title = 'Quotation';
    }

    public function save()
    {

        if($this->quotation->id == null)
        {
            Quotation::create([
                'user_id' => $this->user,
                'company_id' => $this->company,
                'comp_id' => $this->comp,
                'number' => $this->quotation->quotationNumber($this->comp),
                'date' => $this->quotation->date,
                'title' => $this->quotation->title,
                'to' => $this->quotation->to,
                'address' => $this->quotation->address,
                'description' => $this->quotation->description,
                'delivery' => $this->quotation->delivery ?? 0,
                'validity' => $this->quotation->validity ?? 0,
                'costing' => $this->quotation->costing,
            ]);

        }//Existing Quotation - Edit Quotation
        else{
            $this->quotation->fill([
                'user_id' => $this->quotation->user_id,
                'company_id' => $this->quotation->company_id,
                'comp_id' => $this->quotation->comp_id,
                'number' => $this->quotation->number,
                'date' => $this->quotation->date,
                'title' => $this->quotation->title,
                'to' => $this->quotation->to,
                'address' => $this->quotation->address,
                'description' => $this->quotation->description,
                'delivery' => $this->quotation->delivery,
                'validity' => $this->quotation->validity,
                'costing' => $this->quotation->costing,
            ]);

            $this->quotation->save();
        }

        return redirect()->route('employees.quotations.index');
    }

    public function render()
    {
        return view('livewire.quotation-form');
    }
}

 <div class="col-span-12 xl:col-span-6">
                <x-jet-label for="user" value="{{ __('CUSTOMER') }}" class="font-bold text-left" />
                <select name="user" wire:model="user"
                    class="block w-full mt-1 border-gray-300 rounded-md shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50">
                    <option value='' selected>Choose a Customer</option>
                    @foreach($users as $user)
                        <option value={{ $user->id }}>{{ $user->name }}</option>
                    @endforeach
                </select>
                <x-jet-input-error for="user" class="mt-2" />
            </div>

            <div class="col-span-12 xl:col-span-6">
                <x-jet-label for="company_id" value="{{ __('COMPANY') }}" class="font-bold text-left" />
                <select name="company" wire:model="company" class="block w-full mt-1 border-gray-300 rounded-md shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50">
                    {{-- @if ($companies->count() == 0)
                        <option value="">Choose company</option>
                    @endif --}}
                    @foreach($companies as $company)
                        <option value="{{ $company->id }}">{{ $company->name }}</option>
                    @endforeach
                </select>
                <x-jet-input-error for="company" class="mt-2" />
            </div>

         <div class="col-span-12 py-6 xl:col-span-4">
                <x-success-button class="w-full" type="submit">
                    {{ $quotation->id ? 'Update' : 'Save' }}
                </x-success-button>
            </div>

这是我期望发生的

 //Dependant Dropdown - Customer Company in Edit Mode
        if($quotation->count() > 1)
        {
            $this->user = $this->quotation->user_id;
            //Expecting to have the company selected in the edit mode
            $this->company = $this->quotation->company_id; 
        }

标签: laravellaravel-livewire

解决方案


推荐阅读