路线: \ routes \ web.php
Route::get('api/get-market-list','MemberTradesController@getMarketList');
Route::get('api/get-market-list1','MemberTradesController@getMarketListtest');
控制器 MemberTradesController
有两个功能, getMarketListtest (仅用于在服务器上进行测试)和 getMarketList (这是针对Request $请求,因为我在相关选择框中使用了它)
public function getMarketListtest(){
$markets = DB::table("markets")
->pluck("market","id");
return response() -> json($markets);
}
public function getMarketList(Request $request){
$markets = DB::table("markets")
->where("exchange_id", $request->exchange_id)
->pluck("market","id");
return response() -> json($markets);
}
Java脚本:
<script type="text/javascript">
$('#exchange').change(function(){
var exchangeID = $(this).val();
if(exchangeID){
$.ajax({
type:"GET",
url:"{{url('api/get-market-list')}}?exchange_id="+exchangeID,
success:function(res){
if(res){
$("#market").empty();
$("#market").append('<option>Select</option>');
$.each(res,function(key,value){
$("#market").append('<option value="'+key+'">'+value+'</option>');
});
}else{
$("#market").empty();
}
}
});
}else{
$("#market").empty();
$("#symbol").empty();
}
});
</script>
查看:
<title>Laravel 5 - Dynamic autocomplete search using select2 JS Ajax</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
{!! Form::open(['method'=>'POST', 'action'=> 'MemberTradesController@store']) !!}
<div class="form-group col-sm-5">
{!! Form::label('exchange_id', 'Exchanges:') !!}
{!! Form::select('exchange_id', [''=>'Choose Options'] + $exchanges , null, ['class'=>'form-control', 'id'=>'exchange'])!!}
</div>
<div class="form-group col-sm-5">
{!! Form::label('market_id', 'Markets:') !!}
{!! Form::select('market_id', [''=>'Choose Options'] , null, ['class'=>'form-control', 'id'=>'market'])!!}
</div>
{!! Form::close() !!}
TABLE的屏幕截图:
对于功能二Link for API result : api/get-market-list
重要信息:
[根据您的功能更改了代码] 4
答案 0 :(得分:0)
您可以使用$request->input('exchange_id');
代替$request->exchange_id
试着替换这个功能。
public function getMarketList(\Illuminate\Http\Request $request){
$exchange_id = $request->input('exchange_id');
if(!empty($exchange_id)){
\Log::info('This is working.');
}
$markets = DB::table("markets")
->where('exchange_id', $exchange_id)
->pluck("market","id");
return response()->json($markets);
}
请评论是否有任何错误。