0

I'm creating REST API with Laravel 5.6 (I have to say I'm new because I might have used the wrong terms. I'm sorry about that,I'm improving myself. I need to hear my faults :) )

I have one function for find nearby places in my controller

     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);
 }

and I'm using this url for send current location but when I giving them , laravel showing to me all parties not nearby.I want to show nearby places

http://127.0.0.1:8000/api/parties?lat=37.043237&long=27.392445

but when I sending my parameter to url it showing

{"data":[]}

I can't show any nearby places

also in my database I'm keeping lat and long like this :

   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();
    });
}

How can I show the nearby ones ?

Baran KARABOGA
  • 80
  • 1
  • 11

2 Answers2

1

I fixed , I hope it will help somebody

class PartiesController extends Controller
{
        public function index(\Illuminate\Http\Request $request) {
          if($request->has('lat') && $request->has('long')){
                $lat = $request->lat;
                $long = $request->long; 
            $parties=DB::select(DB::raw("SELECT *,111.045*DEGREES(ACOS(COS(RADIANS(':lat'))*COS(RADIANS(`latitude`))*COS(RADIANS(`longitude`) - RADIANS(':long'))+SIN(RADIANS(':lat'))*SIN(RADIANS(`latitude`)))) AS distance_in_km FROM parties ORDER BY distance_in_km asc LIMIT 0,5"), array(
                'lat' => $lat,
                'long' => $long
              ));
            $hidacik = Parties::hydrate($parties);
            return Fractal::includes('places')->collection($hidacik,new PartyTransformer);
             }
           else {
                $parties = Parties::all();
            }
       return Fractal::includes('places')->collection($parties,new PartyTransformer);
        }
}
Baran KARABOGA
  • 80
  • 1
  • 11
0

In $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"); you are missing ->get(). you need to add get() in order to return a collection which you can then work with

 //this returns a collection now since we added get()  
 $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")->get();
flex_
  • 679
  • 2
  • 8
  • 26