laravel API,不从数据库

时间:2018-05-08 10:08:14

标签: laravel laravel-5.3

这是生产方面(在unbtu数字海洋服务器上)

  • “laravel:5.3”
  • “php”:“5.5.9”

路径路径:/route/web.php(不在中间件中)

Route::get('api/getsymbol-request','MemberTradesController@getSymbolRequest');
Route::get('api/getsymbol','MemberTradesController@getSymbol'); 

控制器:

public function getSymbolRequest(Request $request){
        $symbol = DB::table("symbols")
            ->where("market_id", $request->market_id)
            ->pluck("symbol","id");
        return response() -> json($symbol);
    }

public function getSymbol(){
        $symbol = DB::table("symbols")
            ->pluck("symbol","id");
        return response() -> json($symbol);
    }

http://yourtradelog.com/api/getsymbol-request?market_id=1 //这是无法正常工作的网址我遇到了问题为什么这不起作用

http://yourtradelog.com/api/getsymbol //这是工作网址

Database tables

    <script>
        $(document).ready(function() {

            $('#exchange').change(function(){
                var exchangeID = $(this).val();
                if(exchangeID){
                    $.ajax({
                        type:"GET",
                        url:"{{url('api/getsymbol-request/')}}?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();
                }
            });

        });
    </script>
     {!! Form::open(['method'=>'POST', 'action'=> 'MemberTradesController@store']) !!}
 <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>

        <div class="form-group col-sm-10">
            {!! Form::label('symbol_id', 'Symbol:') !!}
            {!! Form::select('symbol_id', [''=>'Choose Options'] ,  null, ['class'=>'symbol_id', 'id'=>'symbol','data-width'=>'60%', 'data-live-search'=>'true'] )!!}
        </div>
 
 <div class="form-group col-lg-4">
                {!! Form::submit('Add trade', ['class'=>'btn btn-success btn-lg']) !!}
            </div>

            {!! Form::close() !!}

2 个答案:

答案 0 :(得分:2)

我更喜欢使用这样的路由参数(请参阅文档here中的更多内容):

路线:

Route::get('api/getsymbol-request/{id}','MemberTradesController@getSymbolRequest');
Route::get('api/getsymbol','MemberTradesController@getSymbol'); 

控制器:

public function getSymbolRequest(Request $request, $id){
        $symbol = DB::table("symbols")
            ->where("market_id", $id)
            ->get()
            ->pluck("symbol","id");
        return response() -> json($symbol);
    }

public function getSymbol(){
        $symbol = DB::table("symbols")
            ->pluck("symbol","id");
        return response() -> json($symbol);
    }
  

请注意,网址已更改为以下格式:

     

http://yourtradelog.com/api/getsymbol-request/1

答案 1 :(得分:0)

尝试使用此代码:

public function getSymbolRequest(Request $request)
{
    $marketID = (int)$request->get('market_id')
    $symbol = DB::table('symbols')
        ->where('market_id', $marketID)
        ->get(['symbol','id']);
    return response()->json($symbol);
}
public function getSymbol()
{
    $symbol = DB::table('symbols')
        ->get(['symbol','id']);
    return response()->json($symbol);
}