Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TimToronto's avatar

Variable not being recognized

Hello, I've only been using Laravel for a couple of days so this is hopefully a basic error. I've created a form that submits checkbox values as an array. I need to get the array than loop through it. The code in my controller looks like this:

$tagschecked = Input::get('stag');
        $polaroids = Polaroid::where('id','>',0);
        foreach( $tagschecked as $tchecked )
            {
               $polaroids = $polaroids->whereHas('tags', function($q)
                {
                    $q->where('id', '=', $tchecked);

                });
            }

The error I'm getting is : Undefined variable: tchecked

The error page confirms that the array is coming through though: stag Array ( [0] => 91 [1] => 5 )

Any ideas why my tchecked variable isn't being recognized?

0 likes
2 replies
Croisciento's avatar
Level 1

If you want to use a variable then you should do it like this :

$polaroids = $polaroids->whereHas('tags', function($q) use($tchecked)
                {
                    $q->where('id', '=', $tchecked);

                });

Please or to participate in this conversation.