Php Laravel:Mysql查询非常有说服力

Php Laravel:Mysql查询非常有说服力,php,mysql,laravel,laravel-5.4,Php,Mysql,Laravel,Laravel 5.4,我怎样才能以雄辩的形式写出下面的问题?此查询将根据搜索词为我提供广告,然后对数据进行排序以显示最近的位置。$latitude和$longitude将是GET参数。我的表格关系是LIKE,其中tbl\u people表格与tbl\u ads表格和位置相关。查询工作正常,现在我需要这个有说服力的查询 SELECT tbl_ads.Title, tbl_ads.Description, tbl_ads.Sub_Category_Id, tbl_ads.Image, tbl_ads.

我怎样才能以雄辩的形式写出下面的问题?此查询将根据搜索词为我提供广告,然后对数据进行排序以显示最近的位置。
$latitude
$longitude
将是
GET
参数。我的表格关系是
LIKE
,其中
tbl\u people
表格与
tbl\u ads
表格和位置相关。查询工作正常,现在我需要这个有说服力的查询

SELECT
  tbl_ads.Title,
  tbl_ads.Description,
  tbl_ads.Sub_Category_Id,
  tbl_ads.Image,
  tbl_ads.Date_Time,
  tbl_ads.Main_Category_Id,
  tbl_ads.People_Id,
  tbl_location.Address,
  ROUND((
    6371 * ACOS(
      COS(RADIANS($latitude)) * COS(RADIANS(tbl_location.Latitude)) * COS(
        RADIANS(tbl_location.Longitude) - RADIANS($longitude)
      ) + SIN(RADIANS($latitude)) * SIN(RADIANS(tbl_location.Latitude))
    )
  ),2) AS DISTANCE
FROM
  tbl_ads
INNER JOIN
  tbl_people ON tbl_ads.People_Id = tbl_people.Id
INNER JOIN
  tbl_location ON tbl_people.Location_ID = tbl_location.Id
WHERE
  (
    tbl_ads.Title LIKE '%for%' OR tbl_ads.Description LIKE '%for%'
  )
到目前为止,我已经用如下的原始格式编写了这个查询,但不知道如何包含
距离
列。顺便说一下,这个查询运行得很好

DB::表('用户') ->join('posts','users.id','=','posts.user_id')) ->join('locations','users.id','=','locations.id') ->选择('posts.title'、'users.name'、'locations.address') ->get()


Laravel原始格式的MySQL查询:

DB::table('tbl_people')->join('tbl_ads', 'tbl_people.id', '=', 'tbl_ads.people_id')->join('tbl_location', 'tbl_people.id', '=', 'tbl_location.id')->select('tbl_ads.*
', 'tbl_people.*', 'tbl_location.address', DB::raw('ROUND((6371 * ACOS(COS(RADIANS(31.430443800000003)) * COS(RADIANS(tbl_location.lattitude)) * COS(RA
DIANS(tbl_location.longitude) - RADIANS(74.280726)) + SIN(RADIANS(31.430443800000003)) * SIN(RADIANS(tbl_location.lattitude)))),2) AS DISTANCE'))-
>where('tbl_ads.description','like','%for%')->orwhere('tbl_ads.title', 'like', '%for%')->get();

Laravel原始格式的MySQL查询:

DB::table('tbl_people')->join('tbl_ads', 'tbl_people.id', '=', 'tbl_ads.people_id')->join('tbl_location', 'tbl_people.id', '=', 'tbl_location.id')->select('tbl_ads.*
', 'tbl_people.*', 'tbl_location.address', DB::raw('ROUND((6371 * ACOS(COS(RADIANS(31.430443800000003)) * COS(RADIANS(tbl_location.lattitude)) * COS(RA
DIANS(tbl_location.longitude) - RADIANS(74.280726)) + SIN(RADIANS(31.430443800000003)) * SIN(RADIANS(tbl_location.lattitude)))),2) AS DISTANCE'))-
>where('tbl_ads.description','like','%for%')->orwhere('tbl_ads.title', 'like', '%for%')->get();

您需要使用model方法还是只使用DB::select()?您可能必须将其作为原始查询。两者都可以。您需要使用model方法还是只使用DB::select()?您可能必须将其作为原始查询。两者都可以。