1

I have the following code in a Laravel controller, but the given parameter variable $idea is undefined in the 'whereIn' function. How can I access $idea there? Thank you!

public static function getIdea($idea = null) {
    if ($idea != "") {
        return DB::table('test')->select('foo')
            ->whereIn('bar', function($query)
              {
                  $query->select('foobar')
                        ->from('bar2')
                        ->where('foo2', '=', $idea)
                        ->get();
              })
            ->get();
    }
}

1 Answer 1

3

Use the use keyword! See Example #3 Inheriting variables from the parent scope on anonymous functions

public static function getIdea($idea = null) {
    if ($idea != "") {
        return DB::table('test')->select('foo')
            ->whereIn('bar', function($query) use ($idea)
              {
                  $query->select('foobar')
                        ->from('bar2')
                        ->where('foo2', '=', $idea)
                        ->get();
              })
            ->get();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.