我要创建一个从属下拉列表,有2个模型应列出其数据并将其保存在Account表中。i在模型中正确建立了它们的关系。在第一个选择选项中,我将“人员”和“员工”设为静态。为获取数据创造了条件到目前为止,它已经以某种方式起作用了,但是我想知道如何将数据存储在控制器中。任何帮助将不胜感激。
Account Model
public function accountable()
{
return $this->morphTo();
}
Employee Model
class Employee extends Model
{
protected $guarded = [];
public function account()
{
return $this->morphOne(Account::class, 'accountable');
}
}
Person Model
class Person extends Model
{
protected $guarded = [];
public function accounts()
{
return $this->morphMany(Account::class, 'accountable');
}
}
这是表格
<div class="form-group">
<label class="col-md-3 control-label">Accountable</label>
<div class="col-md-5">
<select class="form-control select2me" name="accountable_type">
<option value=""></option>
<option value="person">Person</option>
<option value="employee">Employee</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Accountable_Type</label>
<div class="col-md-5">
<select name="accountable_id" class="form-control select2me">
{{-- <option value=""></option> --}}
</select>
</div>
</div>
这是ajax
<script>
$(document).ready(function() {
$('select[name="accountable_type"]').on('change', function() {
var person_id = $(this).val();
if(person_id == 'person') {
$.ajax({
url: "{{URL('get_person')}}",
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="accountable_id"]').empty();
$.each(data, function(key, value) {
$('select[name="accountable_id"]').append('<option value="'+ key +'">'+
value.name +'</option>');
});
}
});
}else{
$('select[name="accountable_id"]').empty();
}
});
$('select[name="accountable_type"]').on('change', function() {
var employee_id = $(this).val();
if(employee_id == 'employee') {
$.ajax({
url: "{{URL('get_employee')}}",
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="accountable_id"]').empty();
$.each(data, function(key, value) {
$('select[name="accountable_id"]').append('<option value="'+ key +'">'+
value.name +'</option>');
});
}
});
}else{
$('select[name="accountable_id"]').empty();
}
});
});
</script>