为了查询表,我使用了DB facade。我想在返回结果之前操纵列值。 我在模态中使用了eloquent accessors getAttribute()方法,但它没有做什么
public function findRestaurantByCordinates($request)
{
$numPerPage = 6;
$page = ($request->input('page')) ? $request->input('page') : 1;
$sql = "r.*, ( 6371 * acos( cos( radians(".$request->input('lat').") ) * cos( radians( res_lat ) ) * cos( radians( res_lon ) - radians(".$request->input('lang').") ) + sin( radians(".$request->input('lat').") ) * sin( radians( res_lat ) ) ) ) AS distance";
/*custom query for results*/
$result = DB::table('restaurants as r');
if($request->input('lat') && $request->input('lang'))
{
$result = $result->select(DB::raw($sql));
$result = $result->having('distance', '<', 20);
}
if(!empty($request->input('features')))
{
$result = $result->join('res_features', 'r.restaurant_id', '=', 'res_features.restaurant_id');
}
if(!empty($request->input('tags')))
{
$result = $result->join('res_tags', 'r.restaurant_id', '=', 'res_tags.restaurant_id');
}
if(!empty($request->input('features')))
{
$result = $result->whereIn('res_features.feature_id', $request->input('features'));
}
if(!empty($request->input('tags')))
{
$result = $result->whereIn('res_tags.tag_id', $request->input('tags'));
}
$result = $result
->groupBy('r.restaurant_id')
->get();
// Start displaying items from this number;
$offSet = ($page * $numPerPage) - $numPerPage; // Start displaying items from this number
//dd($offSet);
// Get only the items you need using array_slice (only get 10 items since that's what you need)
$itemsForCurrentPage = array_slice($result, $offSet, $numPerPage, true);
//dd($itemsForCurrentPage);
$pagination = new LengthAwarePaginator($itemsForCurrentPage, count($result), $numPerPage, $page, ['path' => $request->url(), 'query' => $request->query()]);
return $pagination;
}
此方法返回json,如
[{"restaurant_id":63,"res_title":"Foruche ","res_title_jp":"フォルチェ","city_id":0,"pre_id":22,"res_address":"1-5-2 Dogenzaka Shibuya Tokyo","res_address_jp":"東京都 渋谷区 道玄坂 1-5-2 渋谷SEDEビル 5F","res_lat":"35.6580514","res_lon":"139.6973349","res_overview":"","res_overview_jp":"","res_phone_1":"050-5868-2467 (+81-50-5868-2467) ","res_phone_2":"03-3476-7776 (+81-3-3476-7776) ","res_fax":"","res_email":"","res_website":"","lunch_start_price":0,"lunch_end_price":0,"dinner_start_price":0,"dinner_end_price":0,"occupancy":"","operating_hour":"17:30~翌2:00 15:00~23:00","res_status":"","created_by":0,"updated_by":0,"created_at":"0000-00-00 00:00:00","updated_at":"0000-00-00 00:00:00","slug":"test","distance":6.4750019625656},{"restaurant_id":65,"res_title":"Bishokumaimon ","res_title_jp":"美食 米門 渋谷店","city_id":0,"pre_id":22,"res_address":"Tokyo, Shibuya-ku, Udagawa-cho, 15-1 Shibuya Parco PART1 8F","res_address_jp":"東京都 渋谷区 宇田川町 15-1 渋谷パルコ PART1 8F","res_lat":"35.6619563","res_lon":"139.6967115","res_overview":"","res_overview_jp":"","res_phone_1":"050-5869-7460 (+81-50-5869-7460)","res_phone_2":"03-4577-6648 (+81-3-4577-6648)","res_fax":"","res_email":"","res_website":"","lunch_start_price":0,"lunch_end_price":0,"dinner_start_price":0,"dinner_end_price":0,"occupancy":"","operating_hour":" 11:00 ~ 15:00 17:30 ~ 23:30","res_status":"","created_by":0,"updated_by":0,"created_at":"0000-00-00 00:00:00","updated_at":"0000-00-00 00:00:00","slug":"test","distance":6.1275693867962},{"restaurant_id":67,"res_title":"GANEZA ","res_title_jp":"ガネーシャ 日本橋浜町店","city_id":907,"pre_id":41,"res_address":"2-30-6 Nihonbashihamacho Chuo Tokyo","res_address_jp":"東京都 中央区 日本橋浜町 2-30-6 花の東京ビル 1F","res_lat":"35.6873505","res_lon":"139.7854157","res_overview":"","res_overview_jp":"","res_phone_1":"","res_phone_2":"+81-3-5623-5460","res_fax":"","res_email":"","res_website":"","lunch_start_price":999,"lunch_end_price":0,"dinner_start_price":1000,"dinner_end_price":1999,"occupancy":"38 seats","operating_hour":"(ランチ)11:00~15:00
(ディナー)16:00~22:00
Lunch","res_status":"A","created_by":0,"updated_by":0,"created_at":"0000-00-00 00:00:00","updated_at":"2016-07-03 23:16:19","slug":"test","distance":5.3927689064301},{"restaurant_id":68,"res_title":"Nihonkaishouya ","res_title_jp":"日本海庄や 町屋店","city_id":0,"pre_id":22,"res_address":"6-6-1 Arakawa Arakawa Tokyo","res_address_jp":"東京都 荒川区 荒川 6-6-1","res_lat":"35.7427213","res_lon":"139.777968","res_overview":"","res_overview_jp":"","res_phone_1":"050-5571-0368 (+81-50-5571-0368) ","res_phone_2":"03-5901-4631 (+81-3-5901-4631) ","res_fax":"","res_email":"","res_website":"","lunch_start_price":0,"lunch_end_price":0,"dinner_start_price":0,"dinner_end_price":0,"occupancy":"","operating_hour":"11:30~23:30","res_status":"","created_by":0,"updated_by":0,"created_at":"0000-00-00 00:00:00","updated_at":"0000-00-00 00:00:00","slug":"test","distance":5.5912895038939},{"restaurant_id":77,"res_title":"Jitokkokumiai ","res_title_jp":"じとっこ組合 世田谷経堂店","city_id":0,"pre_id":22,"res_address":"1-25-17 Kyodo Setagaya Tokyo","res_address_jp":"1-25-17 Kyodo Setagaya Tokyo","res_lat":"35.6490287","res_lon":"139.6340795","res_overview":"","res_overview_jp":"","res_phone_1":"050-5868-3517 (+81-50-5868-3517)","res_phone_2":"03-6413-1940 (+81-3-6413-1940)","res_fax":"","res_email":"","res_website":"","lunch_start_price":0,"lunch_end_price":0,"dinner_start_price":0,"dinner_end_price":0,"occupancy":"","operating_hour":"17:00~1:00 17:00~24:00","res_status":"","created_by":0,"updated_by":0,"created_at":"0000-00-00 00:00:00","updated_at":"0000-00-00 00:00:00","slug":"test","distance":11.07793356266},{"restaurant_id":92,"res_title":"Kagoshimakenkirishimashitsukadanoujou ","res_title_jp":"鹿児島県霧島市 塚田農場 中目黒店","city_id":0,"pre_id":22,"res_address":"1-20-5 Kamimeguro Meguro Tokyo","res_address_jp":"東京都 目黒区 上目黒 1-20-5 エーワンビル","res_lat":"35.6446883","res_lon":"139.696901","res_overview":"","res_overview_jp":"","res_phone_1":"050-5570-7183 (+81-50-5570-7183)","res_phone_2":"050-5570-7183 (+81-50-5570-7183)","res_fax":"","res_email":"","res_website":"","lunch_start_price":0,"lunch_end_price":0,"dinner_start_price":0,"dinner_end_price":0,"occupancy":"","operating_hour":"17:00~24:00","res_status":"","created_by":0,"updated_by":0,"created_at":"0000-00-00 00:00:00","updated_at":"0000-00-00 00:00:00","slug":"test","distance":7.8247499035529}]
所以现在我想要的是在json结果中你可以看到我需要操作的operating_hour列只想替换像〜或者回复的一些字符。
注意:事情是我只想使用JSON.parse()解析那些json结果,但它会引发一些错误。我只是认为可能是因为那些我无法解析它的角色这就是为什么我想使用Accessor但它对我没有帮助。您可以尝试解析该json结果。需要帮助。