Php 在Laravel中编辑和填充相关下拉菜单
我为Laravel创建了一个相关的下拉菜单,它连接国家和州,非常适合Php 在Laravel中编辑和填充相关下拉菜单,php,mysql,laravel,laravel-5,drop-down-menu,Php,Mysql,Laravel,Laravel 5,Drop Down Menu,我为Laravel创建了一个相关的下拉菜单,它连接国家和州,非常适合create.blade.php,没有任何问题 使用的Javascript: jQuery(document).ready(function () { jQuery('select[name="country"]').on('change',function(){ var countryID = jQuery(this).val(); if(countryID)
create.blade.php
,没有任何问题
使用的Javascript:
jQuery(document).ready(function ()
{
jQuery('select[name="country"]').on('change',function(){
var countryID = jQuery(this).val();
if(countryID)
{
jQuery.ajax({
type : "GET",
url : '<?php echo url('/'); ?>/admin/country/' + (countryID) + '/states/',
dataType : "json",
success:function(data)
{
console.log(data);
jQuery('select[name="state"]').empty();
$('select[name="state"]').append('<option value="">{{ trans('global.address.placeholder.state') }}</option>');
jQuery.each(data, function(key,value){
$('select[name="state"]').append('<option value="'+ key +'">'+ value +'</option>');
});
}
});
}
else
{
$('select[name="state"]').empty();
}
});
});
在edit.blade.php
中,我正在使用:
@foreach ($states as $state)
<option value="{{ $state->id }}" {{$address->state == $state->id ? 'selected' : ''}}>{{ $state->name}}</option>
@endforeach
@foreach($states为$state)
state==$state->id?“所选“:”}>{{$state->name}
@endforeach
从数据库中检索所选数据。到目前为止,一切都是完美的,但是在编辑部分,它在DB中填充表格状态中的所有内容,跳过国家ID的依赖项。例如,它显示世界上所有的州,而不是仅显示美国,因为美国是创建时选择的国家。我被困住了找不到解决办法有人能帮忙吗
我一直在考虑的另一件事是,将MySQL数据库中dependent下拉列表的连接更改为连接到资源文件,例如resources>lang>en>countries.php和states.php,目的是优化数据库查询的负载以获得更好的性能。有什么建议吗
@foreach ($states as $state)
<option value="{{ $state->id }}" {{$address->state == $state->id ? 'selected' : ''}}>{{ $state->name}}</option>
@endforeach