Tud*_*rbu 6 laravel laravel-nova
我有这个资源叫 Distributor
ID::make()->sortable(),
Text::make('Name')
->creationRules('required'),
BelongsTo::make('Region')
->creationRules('required')
->searchable(),
BelongsTo::make('Country')
->creationRules('required')
->searchable(),
Run Code Online (Sandbox Code Playgroud)
一切都到位了。但是Country模型应该取决于Region模型,所以当我选择一个地区时,我想显示与该地区相关的国家的选项。
Region 和 Country 在基于belongsToMany关系的模型中已经相关。
有没有办法让这些领域一起工作?
我意识到这个问题已经有将近一年的历史了,但我想我会回答为 1. 这个问题仍然有流量,2. 我们最近遇到了一个相同的问题,并对缺乏可用信息感到失望。
据我所知,这个问题也可以通过相关查询来解决,但由于各种原因,我们最终添加了自定义字段。自定义字段的官方文档非常稀疏,但应该足以入门。
我们的自定义字段在 Vue 方面仍然非常简单。Vue 处理的唯一真正逻辑是从我们的 API 中提取国家/地区,并将其填充到下拉列表中。在 PHP 方面,我们最终需要重写字段控制器中的两个函数:fillAttributeFromRequest() 和resolve()。见下文:
国家/地区.php:
namespace Gamefor\CountryState;
use Laravel\Nova\Fields\Field;
class CountryState extends Field
{
public $component = 'country-state';
/**
* Hydrate the given attribute on the model based on the incoming request.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param string $requestAttribute
* @param object $model
* @param string $attribute
* @return void
*/
protected function fillAttributeFromRequest($request, $requestAttribute, $model, $attribute)
{
parent::fillAttributeFromRequest($request, $requestAttribute, $model, $attribute);
if ($request->exists('state_id')) {
$model->state_id = $request['state_id'];
}
if ($request->exists('country_id')) {
$model->country_id = $request['country_id'];
}
}
/**
* Resolve the field's value for display.
*
* @param mixed $resource
* @param string|null $attribute
* @return void
*/
public function resolve($resource, $attribute = null)
{
// Model has both country_id and state_id foreign keys
// In the model, we have
//
// public function country(){
// return $this->belongsTo('App\Country', 'country_id', 'id');
// }
//
// public function state(){
// return $this->belongsTo('App\State', 'state_id', 'id');
// }
$this->value = $resource->country['name'] . ', ' . $resource->state['name'];
}
}
Run Code Online (Sandbox Code Playgroud)
表单字段.vue
<template>
<default-field :field="field" :errors="errors">
<template slot="field">
<select
name="country"
ref="menu"
id="country"
class="form-control form-select mb-3 w-full"
v-model="selectedCountryId"
@change="updateStateDropdown"
>
<option
:key="country.id"
:value="country.id"
v-for="country in countries"
>
{{ country.name }}
</option>
</select>
<select
v-if="states.length > 0"
name="state"
ref="menu"
id="state"
class="form-control form-select mb-3 w-full"
v-model="selectedStateId"
>
<option :value="state.id" :key="state" v-for="state in states">
{{ state.name }}
</option>
</select>
</template>
</default-field>
</template>
<script>
import { FormField, HandlesValidationErrors } from "laravel-nova";
export default {
mixins: [FormField, HandlesValidationErrors],
props: {
name: String
},
data() {
return {
countries: [],
states: [],
allStates: [],
selectedCountryId: null,
selectedStateId: null
};
},
created: function() {
this.fetchCountriesWithStates();
},
methods: {
updateStateDropdown() {
this.states = this.allStates.filter(
item => item.country_id === this.selectedCountryId
);
this.selectedStateId = this.states.length > 0 ? this.states[0].id : null;
},
async fetchCountriesWithStates() {
const countryResponse = await Nova.request().get("/api/v1/countries");
const stateResponse = await Nova.request().get("/api/v1/states");
this.countries = countryResponse.data;
this.allStates = stateResponse.data;
this.updateStateDropdown();
},
fill(formData){
formData.append('country_id', this.selectedCountryId);
formData.append('state_id', this.selectedStateId);
},
},
};
</script>
Run Code Online (Sandbox Code Playgroud)
IndexField.vue
<template>
<span>{{ field.value }}</span>
</template>
<script>
export default {
props: ['resourceName', 'field',],
}
</script>
Run Code Online (Sandbox Code Playgroud)
最后,在我们的 Nova 资源的字段数组中:
CountryState::make('Country and State')->rules('required')
Run Code Online (Sandbox Code Playgroud)
这些样本在“生产就绪”之前肯定需要进行调整,但希望它们能帮助任何敢于冒险进入 Nova 定制这个狂野兔子洞的人。
| 归档时间: |
|
| 查看次数: |
4017 次 |
| 最近记录: |