我如何在Laravel中使用BETWEEN和AND [英] How do I use BETWEEN and AND in laravel

查看:4693
本文介绍了我如何在Laravel中使用BETWEEN和AND的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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'

我正在尝试编写与

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'

推荐答案

如果只想构建查询/获取纯数据而没有任何逻辑,则只需使用查询生成器即可:

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

关于您如何雄辩地使用AND的问题的一些补充说明:

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

AND.所以

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 = ?

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

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:

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

这篇关于我如何在Laravel中使用BETWEEN和AND的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆