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 ?

9
  • 1
    have you tried your query in database tool like mysql? Commented Apr 12, 2018 at 3:39
  • What problems are you running into? Commented Apr 12, 2018 at 3:45
  • try the query in mysql or whatever db ure using and then tell us the results. Commented Apr 12, 2018 at 4:20
  • check this one quite good solution in my opinion stackoverflow.com/questions/14254641/… Commented Apr 12, 2018 at 4:29
  • @PatrickMoore json result is always showing {"data":[]} :/ . Commented Apr 12, 2018 at 11:07

2 Answers 2

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);
        }
}
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

thank your help I added but I have still same result.
@BaranKARABOGA then this is most likely a problem with the query itself.. try to somehow run it in mysql and see if it returns anything

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.