user8504877
user8504877

Reputation:

Use Variable in Eloquent whereIn Clause

whats the right way to use a variable in eloquent whereIn Clause?

For example this is my string variable

$testVar = 'Dave, Tom, Brad';

And here is my eloquent Query.

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

When I put this $testVar into the Query I get back an empty result. But this is not correct. Because I have the values Dave Tom and Brad.

Upvotes: 0

Views: 740

Answers (1)

Vision Coderz
Vision Coderz

Reputation: 8078

In your case $testVar is string then you have to convert to array

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

after thay you can pass $myArray into whereIn

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

Upvotes: 1

Related Questions