【发布时间】:2018-04-12 03:23:12
【问题描述】:
我正在使用 Laravel 5.6 创建 REST API(我不得不说我是新手,因为我可能使用了错误的术语。对此我很抱歉,我正在改进自己。我需要听到我的错误: ))
我有一个功能可以在我的控制器中查找附近的地方
public function index(\Illuminate\Http\Request $request) {
if($request->has('party_category')){
$parties = Parties::where('party_category', $request->party_category)->get();//your new query here
}
else if($request->has('lat') && $request->has('long')){
$parties = Parties::whereRaw("ACOS(SIN(RADIANS('latitude'))*SIN(RADIANS($request->lat))+COS(RADIANS('latitude'))*COS(RADIANS($request->lat))*COS(RADIANS('longitude')-RADIANS($request->long)))*6380 < 10");
}else {
$parties = Parties::all();
}
return Fractal::includes('places')->collection($parties,new PartyTransformer);
}
我正在使用这个 url 发送当前位置,但是当我给他们时,laravel 向我显示不在附近的所有各方。我想显示附近的地方
http://127.0.0.1:8000/api/parties?lat=37.043237&long=27.392445
但是当我将参数发送到 url 时,它会显示
{"data":[]}
我无法显示附近的任何地点
在我的数据库中,我也会像这样保持经纬度:
public function up()
{
Schema::create('parties', function (Blueprint $table) {
$table->increments('id');
$table->integer('places_id')->unsigned();
$table->string('slug');
$table->string('party_title');
$table->string('party_category');
$table->string('rating')->nullable();
$table->date('party_date');
$table->string("latitude")->nullable();
$table->string("longitude")->nullable();
$table->integer('fav_count')->nullable();
$table->longText('image_path');
$table->longText('party_desp');
$table->timestamps();
});
}
如何显示附近的?
【问题讨论】:
-
你试过用mysql这样的数据库工具查询吗?
-
你遇到了什么问题?
-
在 mysql 或任何使用的数据库中尝试查询,然后告诉我们结果。
-
在我看来这是一个非常好的解决方案stackoverflow.com/questions/14254641/…
-
@PatrickMoore json 结果总是显示 {"data":[]} :/ .