2014-03-04 47 views
2

HI我嘗試使用下面的和不知道如何得到這個固定如何使用and和在laravel

SELECT * FROM search_users 
WHERE 
    match(first_name,last_name,country,city,location,nationality,short_bio) 
    against (?) 
AND 
    search_users.loc_lng BETWEEN '-0.24272918701172' AND '-0.24272918701172' 
AND 
    search_users.loc_lat BETWEEN '51.47026338272' AND '51.47026338272' 

我想寫一個laravel查詢不完全一樣

select * from search_users 
where 
    ...... 
    and search_users.loc_lng BETWEEN '-0.24272918701172' AND '-0.24272918701172' 
    AND search_users.loc_lat BETWEEN '51.47026338272' AND '51.47026338272' 
+0

不回答你的問題,但值得一提:https://dev.mysql.com/doc/refman/5.0/en /spatial-extensions.html – ficuscr

+0

問題是在laravel中創建查詢而不是如何使用地理位置......對不起,如果我的問題在第一次不明確 – akkarthe

回答

15

如果你只是想周圍沒有任何邏輯來構建查詢/獲取純粹的數據,你可以簡單地使用查詢生成器:

$results = DB::table('search_users') 
      ->where('first_name', $firstname) 
      ->where('last_name', $last_name) 
      ->where('country', $country) //and so on 
      ->whereBetween('loc_lng', array(-0.24272918701172, -0.24272918701172)) 
      ->whereBetween('loc_lat', array(51.47026338272, 51.47026338272)) 
      ->get(); 

千真萬確,你可以使用相同的語法,如果你有一個型號的工作:

$users = User::where('key1', $value1) 
      ->where('key2', $value2) 
      ->whereBetween('loc_lng', array(-0.24272918701172, -0.24272918701172)) 
      ->whereBetween('loc_lat', array(51.47026338272, 51.47026338272)) 
      ->get(); 

關於你如何在雄辯使用AND問題一點點額外的解釋:

AND所使用的如果您使用2個或更多where() s,則默認爲默認值。所以

DB::table('search_users')->where('first_name', $firstname) 
         ->where('last_name', $last_name) 

等於

SELECT * FROM search_users WHERE first_name = ? AND last_name = ? 

OR你可以使用orWhere()

DB::table('search_users')->where('first_name', $firstname) 
         ->orWhere('last_name', $othername) 

相當於

SELECT * FROM search_users WHERE first_name = ? OR first_name = ? 

有時你可能需要更多的東西複雜的,例如:

SELECT * FROM search_users 
WHERE first_name = ? 
    AND (last_name = ? OR last_name = ?) 
    AND age > 27 

雄辯,這將是:

DB::table('search_users') 
     ->where('first_name', $firstname) 
     ->where(function($query) { 
      $query->where('last_name', $lastName1); 
      $query->orWhere('last_name', $lastName2); 
     }) 
     ->where('age', '>', 27)