繁体   English   中英

Laravel - WhereRaw CAST 查询在 while 循环中不起作用

[英]Laravel - WhereRaw CAST query not working within while loop

我有一个 SQL 查询,其中我将一列转换为 SIGNED 以将其与整数进行比较。 我的表“行星”如下所示

id | name | galaxy | region
1  | S1   | 00     | 01
2  | S2   | 00     | 01
3  | S3   | 00     | 02
4  | S4   | 01     | 00
5  | S5   | 00     | 00

有数百个条目属于不同的星系(0-99)和区域(0-99)

星系和地区列被设置为字符串,以用前导 0 存储它们。也许不是最好的方法,但我就是这样做的。

我有一个变量如下

$planet = Planet::where('galaxy', '00')->get();

然后我有下面的循环来带出属于 00 星系和一个定义区域的行星,循环遍历每个可能的区域 (0 - 99) 并打印计数。

$count = 0;
while($count < 100){

   echo $planet->whereRaw('CAST(region as SIGNED) = '.$count)->count();

   $count++;
}

事实是,第一次交互(其中 $count 等于 0)返回结果,但任何后续循环都不会返回任何内容。 如果我将 $count 设置为不同的值,同样的故事 - 第一个循环有效,但没有以下循环。

任何关于为什么会出现这种情况的想法都将不胜感激。 我在循环中 echo'd $count 以仔细检查它肯定会按顺序打印 0 - 99 之间的数字,并且确实如此,所以不确定为什么它不在第一个之外的 SQL 查询中返回结果。

编辑:我想要实现的目标如下图所示:

在此处输入图片说明

基本上有数以千计的行星,每个行星都在一个星系和一个区域中。 星系编号为 00-99,区域也是如此。

我将转到/map/00的 url 告诉我我正在查看哪个星系,然后将出现上面的 10 x 10 网格,每个部分都包含该星系该区域中的行星数量。 每个网格都是在 while 循环中创建的 div。

所以实际的 while 循环如下所示:

$count = 0;
    while($count < 100){
       echo "<div class="inline-flex w-1/10">
       echo $planet->whereRaw('CAST(region as SIGNED) = '.$count)->count();
       echo "</div>
       $count++;
    }

计数就位,所以我们知道我们在网格的哪个部分,因此过滤 $astros->whereRaw 查询以关注该区域。 因为实际表是字符串而不是整数,所以我需要将 is 转换为 SIGNED 字段,以便 where 查询工作。 所以我实际追求的 SQL 查询如下:

SELECT * FROM planets WHERE galaxy = 00 AND CAST(region as SIGNED) = $count

问题 #1:收藏

您不能使用whereRaw() ,因为$planet是集合,因为使用->get()

$planet = Planet::where('galaxy', '00')->get(); // collection

问题#2:AND、AND、AND、...

当您使用$planet->get() ,你会得到许多where结合。

$planet = Planet::where('galaxy', '00');
$count = 0;

while($count < 100){
   echo $planet->whereRaw('CAST(region as SIGNED) = '. $count)->count();

   $count++;
}

结果

select * from `planets` where (`galaxy` = ?) and CAST(region as SIGNED) = 0

select * from `planets` where (`galaxy` = ?) and CAST(region as SIGNED) = 0 and CAST(region as SIGNED) = 1

select * from `planets` where (`galaxy` = ?) and CAST(region as SIGNED) = 0 and CAST(region as SIGNED) = 1 and CAST(region as SIGNED) = 2

select * from `planets` where (`galaxy` = ?) and CAST(region as SIGNED) = 0 and CAST(region as SIGNED) = 1 and CAST(region as SIGNED) = 2 and CAST(region as SIGNED) = 3

select * from `planets` where (`galaxy` = ?) and CAST(region as SIGNED) = 0 and CAST(region as SIGNED) = 1 and CAST(region as SIGNED) = 2 and CAST(region as SIGNED) = 3 and CAST(region as SIGNED) = 4

select * from `planets` where (`galaxy` = ?) and CAST(region as SIGNED) = 0 and CAST(region as SIGNED) = 1 and CAST(region as SIGNED) = 2 and CAST(region as SIGNED) = 3 and CAST(region as SIGNED) = 4 and CAST(region as SIGNED) = 5

....

select * from `planets` where (`galaxy` = ?) and CAST(region as SIGNED) = 0 and CAST(region as SIGNED) = 1 and CAST(region as SIGNED) = 2 and CAST(region as SIGNED) = 3 and CAST(region as SIGNED) = 4 and CAST(region as SIGNED) = 5 and CAST(region as SIGNED) = 6 and CAST(region as SIGNED) = 7 and CAST(region as SIGNED) = 8 and CAST(region as SIGNED) = 9 and CAST(region as SIGNED) = 10 and CAST(region as SIGNED) = 11 and CAST(region as SIGNED) = 12 and CAST(region as SIGNED) = 13 and CAST(region as SIGNED) = 14 and CAST(region as SIGNED) = 15 and CAST(region as SIGNED) = 16 and CAST(region as SIGNED) = 17 and CAST(region as SIGNED) = 18 and CAST(region as SIGNED) = 19 and CAST(region as SIGNED) = 20 and CAST(region as SIGNED) = 21 and CAST(region as SIGNED) = 22 and CAST(region as SIGNED) = 23 and CAST(region as SIGNED) = 24 and CAST(region as SIGNED) = 25 and CAST(region as SIGNED) = 26 and CAST(region as SIGNED) = 27 and CAST(region as SIGNED) = 28 and CAST(region as SIGNED) = 29 and CAST(region as SIGNED) = 30 and CAST(region as SIGNED) = 31 and CAST(region as SIGNED) = 32 and CAST(region as SIGNED) = 33 and CAST(region as SIGNED) = 34 and CAST(region as SIGNED) = 35 and CAST(region as SIGNED) = 36 and CAST(region as SIGNED) = 37 and CAST(region as SIGNED) = 38 and CAST(region as SIGNED) = 39 and CAST(region as SIGNED) = 40 and CAST(region as SIGNED) = 41 and CAST(region as SIGNED) = 42 and CAST(region as SIGNED) = 43 and CAST(region as SIGNED) = 44 and CAST(region as SIGNED) = 45 and CAST(region as SIGNED) = 46 and CAST(region as SIGNED) = 47 and CAST(region as SIGNED) = 48 and CAST(region as SIGNED) = 49 and CAST(region as SIGNED) = 50 and CAST(region as SIGNED) = 51 and CAST(region as SIGNED) = 52 and CAST(region as SIGNED) = 53 and CAST(region as SIGNED) = 54 and CAST(region as SIGNED) = 55 and CAST(region as SIGNED) = 56 and CAST(region as SIGNED) = 57 and CAST(region as SIGNED) = 58 and CAST(region as SIGNED) = 59 and CAST(region as SIGNED) = 60 and CAST(region as SIGNED) = 61 and CAST(region as SIGNED) = 62 and CAST(region as SIGNED) = 63 and CAST(region as SIGNED) = 64 and CAST(region as SIGNED) = 65 and CAST(region as SIGNED) = 66 and CAST(region as SIGNED) = 67 and CAST(region as SIGNED) = 68 and CAST(region as SIGNED) = 69 and CAST(region as SIGNED) = 70 and CAST(region as SIGNED) = 71 and CAST(region as SIGNED) = 72 and CAST(region as SIGNED) = 73 and CAST(region as SIGNED) = 74 and CAST(region as SIGNED) = 75 and CAST(region as SIGNED) = 76 and CAST(region as SIGNED) = 77 and CAST(region as SIGNED) = 78 and CAST(region as SIGNED) = 79 and CAST(region as SIGNED) = 80 and CAST(region as SIGNED) = 81 and CAST(region as SIGNED) = 82 and CAST(region as SIGNED) = 83 and CAST(region as SIGNED) = 84 and CAST(region as SIGNED) = 85 and CAST(region as SIGNED) = 86 and CAST(region as SIGNED) = 87 and CAST(region as SIGNED) = 88 and CAST(region as SIGNED) = 89 and CAST(region as SIGNED) = 90 and CAST(region as SIGNED) = 91 and CAST(region as SIGNED) = 92 and CAST(region as SIGNED) = 93 and CAST(region as SIGNED) = 94 and CAST(region as SIGNED) = 95 and CAST(region as SIGNED) = 96 and CAST(region as SIGNED) = 97 and CAST(region as SIGNED) = 98 and CAST(region as SIGNED) = 99

解决方案#1

$count = 0;

while ($count < 100) {
    $total = Planet::where('galaxy', '00')
        ->whereRaw('CAST(region as SIGNED) = ' . $count)
        ->count();

    echo $total;

    $count++;
}

结果

+--------+-------+
| Region | Count |
+--------+-------+
| 00     | 1     |
| 01     | 2     |
| 02     | 1     |
| 03     | 0     |
| 04     | 0     |

....

| 99     | 0     |
+--------+-------+

解决方案#1(最佳方式,单一查询)

$planets = Planet::select('region', \DB::raw('count(*) as total'))
    ->where('galaxy', '00')
    ->groupBy('region')
    ->pluck('total', 'region');

结果

# region => total
array:3 [
    "00" => 1
    "01" => 2
    "02" => 1
]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM