0

我希望有人可以帮助指导我正确的方向。我是 Laravel 的新手,远非最好的 php 编码器。我正在尝试构建动态嵌套的 where/orWhere 子句,但我被卡住了,卡住的意思是我无法弄清楚。我到目前为止的代码是:

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();

我希望有人能告诉我如何正确地做到这一点,如果上面的代码更接近,我做错了什么。提前感谢您的帮助和指导。

4

0 回答 0