如何在Laravel

时间:2019-10-27 10:00:31

标签: mysql database laravel eloquent

我在数据库中处理了大约20-30k条记录,在对数据进行操作以使其结构合理之后,但是问题是这两个过程需要时间,因此我想通过我的SQL,所以我不需要循环所有获取的数据。

$hotels = \DB::table('hotels')->select(["name","phone","lat", "lng" 
"id","plz","rating","adress","status","website"])->get();
$hotels = $hotels->map(function($elem){
        return ["id" => $elem->id,"name"=>$elem->name,"phone"=>$elem->phone,
        "address"=>$elem->adress,"rating"=>$elem->rating,"website"=>$elem->website,"status" => 
        $elem->status, "plz"=>$elem->plz,"position" => [
            "lat" => $elem->lat,
            "lng" => $elem->lng,
        ]];
    });
return response()->json(['data'=>$hotels]); 

以上查询以这种方式给出响应

0:[address: "Chausseestraße 33, 10115 Berlin, Germany"
id: 1
name: "Hotel Mitte managed by Melia"
phone: "+49 30 254646562"
plz: 10115
position: {lat: 52.5329, lng: 13.3806}]  

是否可以通过数据库查询获得相同的数组,而无需再次循环并创建自定义响应。

0 个答案:

没有答案