url中的查询参数在laravel json响应中不起作用?

时间:2019-09-14 13:40:14

标签: json laravel postman

让我返回了这个json作为Laravel的响应:

Route::get('/data', function(){

    $drivers = Driver::select('driver_number','first_name','ph_number')
                ->where('driver_number',$request->get('driver_number'))
                ->get();

    return response()->json($drivers);
});

以上返回此json:

[
{
    "driver_number": "Dr_01",
    "first_name": "jimale",
    "ph_number": 4253226
},
{
    "driver_number": "Dr_02",
    "first_name": "mawlid",
    "ph_number": 4222321
},
{
    "driver_number": "Dr_03",
    "first_name": "yusuf",
    "ph_number": 3624222
},
{
    "driver_number": "Dr_04",
    "first_name": "yaxye",
    "ph_number": 343243
}
]

因此,我想接收一个json对象,其中driver_number = Dr_02 ...出于这个原因,我使用 POSTMAN 来做到这一点,我这样说:

  

http://localhost/BSProject/public/data?driver_number=Dr_02

但是这将显示所有JSON数组,而不是我的特定json对象。 有谁知道我想念的地方吗?谢谢

1 个答案:

答案 0 :(得分:2)

使用where子句编写过滤查询,如下所示:

public function index(Request $request){
    $drivers = Driver::select('driver_number','first_name','ph_number')->where('driver_number',$request->get('driver_number'))->get();
}