php 我如何在 Laravel 中使用 BETWEEN 和 AND

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22176942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:43:05  来源:igfitidea点击:

How do I use BETWEEN and AND in laravel

phpmysqllaravellaravel-4

提问by akkarthe

HI I am trying to use the following and not sure how to get this fixed

嗨,我正在尝试使用以下内容,但不确定如何解决此问题

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'

I am trying to write a laravelquery that does exactly the same as

我正在尝试编写一个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'

回答by Quasdunk

If you just want to build the query / get pure data without any logic around it, you can simply use the Query Builder:

如果您只想构建查询/获取纯数据而没有任何逻辑,您可以简单地使用查询构建器:

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

And sure enough you can use the same syntax if you're working with a Model:

如果您正在使用模型,当然可以使用相同的语法:

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

A little additional explanation concerning your question about how to use ANDin eloquent:

关于您关于如何AND在 eloquent 中使用的问题的一些额外解释:

ANDis used by default if you use 2 or more where()s. So

AND如果您使用 2 个或更多where()s ,则默认使用。所以

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

equals

等于

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

For ORyou can use orWhere():

因为OR你可以使用orWhere()

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

which equals

等于

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

And sometimes you may need something more complicated, for instance:

有时您可能需要更复杂的东西,例如:

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

In Eloquent, this would be:

在 Eloquent 中,这将是:

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