【问题标题】:Use Variable in Eloquent whereIn Clause在 Eloquent whereIn 子句中使用变量
【发布时间】:2019-08-30 00:55:55
【问题描述】:

在 eloquent whereIn 子句中使用变量的正确方法是什么?

例如这是我的字符串变量

$testVar = 'Dave, Tom, Brad';

这是我雄辩的查询。

$spielerArray = Spieler::join('PlanungSpieler', 'PlanungSpieler.Player_ID', '=', 'Spieler.Player_ID')
            ->whereIn('Spieler.Name', [$testVar])
            ->get();

当我将此 $testVar 放入查询中时,我得到一个空结果。但这是不正确的。因为我有 Dave Tom 和 Brad 的价值观。

【问题讨论】:

    标签: php eloquent where


    【解决方案1】:

    在您的情况下,$testVar 是字符串,那么您必须转换为数组

     $testVar = 'Dave, Tom, Brad';
        $myArray = explode(',', $testVar);
    

    之后你可以将$myArray 传递给whereIn

      $spielerArray = Spieler::join('PlanungSpieler', 'PlanungSpieler.Player_ID', '=', 'Spieler.Player_ID')
                    ->whereIn('Spieler.Name', $myArray )
                    ->get();
    

    【讨论】:

    • 可能与$myArray = array_map('trim', $myArray); 或与, 爆炸
    猜你喜欢
    • 2021-04-16
    • 2017-12-07
    • 2014-08-20
    • 1970-01-01
    • 2019-09-03
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 2020-05-01
    相关资源
    最近更新 更多