如何在laravel 4中实现这一点

时间:2013-06-06 14:33:54

标签: php laravel laravel-4

如何在Laravel 4中实现这一点?

$countryName = DB::only("SELECT f_iptocountry_country_name FROM t_iptocountry WHERE $visitorsIp BETWEEN f_iptocountry_begin_ip_num AND f_iptocountry_end_ip_num");

使用Laravel 3可以查询。

任何?

1 个答案:

答案 0 :(得分:0)

假设您有一个与表t_iptocountry相关的IPToCountry模型,并且您确定不会有多个国家/地区使用相同的IP范围:

$country = IPToCountry::where('f_iptocountry_begin_ip_num', '>=', $visitorsIp)
                            ->where('f_iptocountry_end_ip_num', '<=', $visitorsIp)
                            ->first(['f_iptocountry_country_name']);

$countryName = count($country) > 0 ? $country->f_iptocountry_country_name : '';

您是否关注表现?在这种情况下,要真正使用BETWEEN子句,您有两个选择:一组更复杂的Laravel指令(可能使用匿名函数)或部分使用原始查询。

使用raw成为:

$country = DB::table('t_iptocountry')
                     ->whereRaw("'$visitorsIp' BETWEEN f_iptocountry_begin_ip_num AND f_iptocountry_end_ip_num")
                     ->first(['f_iptocountry_country_name']);

$countryName = count($country) > 0 ? $country->f_iptocountry_country_name : '';