laravel - 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;
}
解决方案
推荐阅读
- scala - In Spark Structured streaming with Kafka, how spark manages offset for multiple topics
- javascript - TypeError: window.require 不是函数 react/electron |__webpack_require__
- node.js - MongoDb 同步问题
- oracle - 如何在 Oracle Apex 的仅显示项目中进行换行?
- swift - 声明一个与实例属性相同的 Swift 类型
- typescript - OpenApiGenerator - 如何因版本更改而跳过生成文件更改
- html - 从引导模式下载内容
- java - Java:格式化包含整数和数组的输出
- unit-testing - 在 Jasmine 中模拟模拟对象的函数调用
- hadoop - 重新启动 HBase 时是否会丢弃 HBase 块缓存?