首页 > 解决方案 > 如何在 Laravel 查询生成器的 Where 子句中使用数组?

问题描述

我想使用数组 $aquiinsidewhere子句,但我不知道该怎么做。我知道如何在where子句中放置多个值,但我不知道arrays.

$aqui[1] = 35;
$aqui[2] = 67;
$aqui[3] = 44;
$aqui[4] = 12;
$aqui[5] = 9;
//and goes...

$caraio = DB::connection('mysql')
->table('users')
->select('name')
->where( /* where fields are equal to $aqui */ )
->value('name');

标签: phplaraveleloquent

解决方案


只需使用:

$aqui[1] = 35;
$aqui[2] = 67;
$aqui[3] = 44;
$aqui[4] = 12;
$aqui[5] = 9;
//and goes...

$caraio = DB::connection('mysql')
->table('users')
->select('name')
->whereIn('attributeName', $aqui)
->value('name');

显然,您想替换attributeName为要比较值的属性的名称。

在此处阅读更多相关信息。

希望这可以帮助!


推荐阅读