Skip to content

Instantly share code, notes, and snippets.

@dynamiccarrots
Created November 1, 2017 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dynamiccarrots/b16a4779df28a83dbde3479fa31f7a52 to your computer and use it in GitHub Desktop.
Save dynamiccarrots/b16a4779df28a83dbde3479fa31f7a52 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers;
use App\Advert;
use App\Libraries\Postcode;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class AdvertController extends Controller
{
/**
* @return \Illuminate\Http\JsonResponse
*/
public function index(Request $request)
{
$circle_radius = 100; // miles
$data = Advert::select('*')->where('active', '=', 1);
if ($request->keyword) {
$data->where([['title', 'like', $request->keyword . '%']]);
}
if ($request->sector) {
$data->where([['title', 'like', $request->keyword . '%']]);
}
if($request->location) {
$location = Postcode::locate($request->location);
$data->addSelect(" ( 3959 * acos( cos( radians('50.866431018898') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('-1.4385519869239') ) + sin( radians('50.866431018898') ) * sin( radians(latitude) ) ) ) AS distance");
}
echo $data->toSql();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment