laravel - Laravel,根据类别选择更改表单字段
问题描述
我的数据库中有 2 个类别,名为“房屋租赁”和“车辆租赁”
用户可以根据这两个类别发布免费广告。
我的清单模型包含以下内容:
Schema::create('listings', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('body');
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('area_id');
$table->unsignedBigInteger('category_id');
$table->boolean('live')->default(false);
$table->softDeletes();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('area_id')->references('id')->on('areas')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
});
当用户访问添加列表表单时,假设他选择“房屋租赁”类别,我希望添加表单显示“大小”、“房间”、“建筑类型”、“is_furnished”、“is_parking”等字段。
同时,如果用户添加列表并选择“车辆租赁”,我希望添加表单显示“vehicle_model”、“year_of_registration”、“condition”等字段。
我该怎么做呢?
解决方案
vue
这是一个可以提供帮助的单个组件
<template>
<div>
<form @submit.prevent="iEdit ? iUpdate(): createMethod()">
<label> Select Rental </label>
<select class="form-control" @change="rentalChanged($event)" v-model="rentalForm.rental_category">
<option value="house rental"> House Rental </option>
<option value="vehicle rental"> Vehicle Rental </option>
</select>
<select v-if="house_rental">
<!-- show house rental options -->
</select>
<select v-if="vehicle_rental">
<!-- show vencle rental options -->
</select>
</form>
</div>
</template>
<script>
name: 'componentName',
data(){
return {
rentalForm: new Form({
rental_category: '',
another_form_field:'',
}),
iEdit : false,
vehicle_rental :false,
house_rental: false,
}
},
methods{
rentalChanged(event){
if(event.target.value == 'house rental'){
this.house_rental = true;
this.vehicle_rental = false;
}
if(event.target.value == 'vehicle rental'){
this.house_rental = false;
this.vehicle_rental = true;
}
},
iUpdate(){
//update method
},
createMethod(){
//create method
},
},
mounted(){
}
</script>
我希望它能给我一个我没有测试过的线索。
推荐阅读
- scala - Scala:访问在另一个文件中定义的案例类
- date - 有没有办法在 pentaho 报表设计器中选择其他参数(条件强制)时强制参数?
- javascript - 当用户开始输入和停止输入时调用一次函数
- swagger - 如何从具有字典作为属性的对象创建 OpenApiObject
- visual-studio - Nuget不安装软件包
- python - Django 运行时错误:模型冲突。将 models.py 文件拆分为模型子文件夹的最佳方法
- python - 未调用 aiortc 通道回调
- spring - 如何将 /actuator/health 中的所有健康检查信息发布到 /actuator/prometheus?
- python-3.x - OMPython 使用带有外部包的自定义模型
- python - Concat Horizontally csv 文件,从多个文件夹中导入它们。文件名如 2021-09-19、2021-09-18... 等)使用 python pandas