0

I hope someone can help guide me in the right direction. I am new to Laravel and far from the best php coder. I am trying to build dynamic nested where/orWhere clauses and I am stuck, and by stuck I mean I can't figure it out. The code I have so far is:

public static function runNested($rId, $current_id) {

    $insidestuff = DB::table('tableName')
        ->select('all fields necessary')
        ...
        ->get();

            $count=1;
            foreach ($insidestuff as $inside)
            {
                $var1 = $inside->var1;
                $var2 = $inside->var2;
                $var3 = $inside->var3;
                $var4 = $inside->var4;

                if ($count == 1)
                {
                    $outsideMet = $outsideMet->where(function($query) use ($var1, $var2, $var3, $var4, $id)
                    {
                        $query->where($var1, $var2, $var3);
                        $outsideMet = myClass::runNested($var4, $id);
                    });
                } else {
                    $outsideMet = $outsideMet->orWhere(function($query) use ($var1, $var2, $var3, $var4, $id)
                    {
                        $query->where($var1, $var2, $var3);
                        $outsideMet = myClass::runNested($var4, $id);
                    }); 
                }
                $count++;

            }
}

public static function runOutside($rId) {

    $outsideQuery = DB::table('tableName')
        ->select('all necessary fields')
        ->get();

    $outsideMet = dbTable::select('*');
    $outsideMet = $outsideMet->leftJoin('necessary fields');

    $outsideCount=1;
    foreach ($oursideQuery as $oQuery)
    {
            $oVar1 = $oQuery->oVar1;
            $oVar2 = $oQuery->oVar2;
            $oVar3 = $oQuery->oVar3;
            $oVar4 = $oVar4;
            $oVar5 = $oQuery->oVar5;

                if ($outsideCount == 1)
                {
                    $outsideMet = $outsideMet->where(function($query) use ($oVar1, $oVar2, $oVar3, $oVar4, $oVar5)
                    {
                        $query->where($oVar1, $oVar2, $oVar3);
                        $outsideMet = myClass::runNested($oVar4, $oVar5);
                    });

                } else {
                    $outsideMet = $outsideMet->orWhere(function($query) use ($oVar1, $oVar2, $oVar3, $oVar4, $oVar5)
                    {
                        $query->where($oVar1, $oVar2, $oVar3);
                        $outsideMet = myClass::runNested($oVar4, $oVar5);
                    });                 
                }
                $outsideCount++;
        }

    $outsideMet = $outsideMet->count();

I hope someone can tell me how to do this properly, and if the above code is even close, what I am doing wrong. Thank you in advance for the help and guidance.

  • [Check this answer, may help you](http://stackoverflow.com/a/19256420/741747). – The Alpha Nov 05 '13 at 19:06
  • There is a typo in `foreach` loop in `runOutside` function. `$oursideQuery` instead of `$outsideQuery`. – tharumax Nov 05 '13 at 22:14
  • The typo unfortunately was not the issue. I don't think the syntax above is correct, specifically the curley brackets and the naming of variables being passed between the nested functions. I hope someone can help push me in the right direction. – user2957559 Nov 07 '13 at 19:05

0 Answers0