laravel-正确使用if语句

时间:2018-12-17 20:11:20

标签: php laravel-5 eloquent

我的代码在这里工作正常,但我需要使其简短。我正在使用if语句,因此选择是使用user_id还是Guest_ip。

但是我得到了很长的代码,有什么帮助吗?

if (Auth::guest()) {

        $Task = Enrollee::with('path.ProgrammingField')->with(['path.pathtags' => function ($q) use ($TagArray)
        {
            $q->with(['Tasks' => function ($q) use ($TagArray)
              {$q->has('tasktags', '=', 2)->orderBy('id', 'ASC') ->whereDoesntHave('tasktags',
                function ($query) use ($TagArray) {
                            $query->whereNotIn('name', $TagArray);
                        }
                    )
               ->with('tasktags');
           }]
        )->orderBy('id', 'ASC');
       }])
        ->where( 'user_id' , '=' , Auth::user()->id )
        ->where('Path_id', $Path->id) ->get();

        $Tasks = $Task[0]->path;
        $Subs = Enrollee::where( 'user_id' , '=' , Auth::user()->id )->where('Path_id', $Path->id)->get();
       $AllSubs = [];
        foreach($Subs as $sub){
            $AllSubs[] = $sub->task_id;
        }
        $AllSubTasks = implode(" ",$AllSubs);
        $SubTasks = explode(",", ($AllSubTasks));
}

       else {

$Task = Enrollee::
with('path.ProgrammingField')

->with(['path.pathtags' => function ($q) use ($TagArray)
{
    $q->with(['Tasks' => function ($q) use ($TagArray)
      {$q->has('tasktags', '=', 2)->orderBy('id', 'ASC')
    ->whereDoesntHave('tasktags',
                function ($query) use ($TagArray) {
                    $query->whereNotIn('name', $TagArray);
                }
            )
       ->with('tasktags');
   }]
)->orderBy('id', 'ASC');
    }])
->where( 'guest_ip' , '=' , '127.0.0.1' )
->where('Path_id', $Path->id) ->get();

$Tasks = $Task[0]->path;
$Subs = Enrollee::where( 'guest_ip' , '=' ,'127.0.0.1') ->where('Path_id', $Path->id)->get();
   $AllSubs = [];
       foreach($Subs as $sub){
            $AllSubs[] = $sub->task_id;
      }
$AllSubTasks = implode(" ",$AllSubs);
$SubTasks = explode(",", ($AllSubTasks));
}

我可以使用

if (Auth::guest()) {

->where( 'guest_ip' , '=' , '127.0.0.1' )
}

我需要成为一个代码,并且如果我使用guest_ip或用户ID进行更改,请使用if语句

2 个答案:

答案 0 :(得分:0)

希望这会对您有所帮助。

$ObjTask = Enrollee::with('path.ProgrammingField')
        ->with(['path.pathtags' => function ($q) use ($TagArray) {
                $q->with(['Tasks' => function ($q) use ($TagArray) {
                        $q->has('tasktags', '=', 2)->orderBy('id', 'ASC')
                        ->whereDoesntHave('tasktags', function ($query) use ($TagArray) {
                                    $query->whereNotIn('name', $TagArray);
                                }
                        )
                        ->with('tasktags');
                    }]
                )->orderBy('id', 'ASC');
            }])
        ->where('Path_id', $Path->id);
$Subs = null;
if (Auth::guest()) {
    $Task  = $ObjTask->where('user_id', '=', Auth::user()->id)->get();    
    $Subs  = Enrollee::where('user_id', '=', Auth::user()->id)->where('Path_id', $Path->id)->get();
}
else {
    $Task  = $ObjTask->where('guest_ip', '=', '127.0.0.1')->get();
    $Subs  = Enrollee::where('guest_ip', '=', '127.0.0.1')->where('Path_id', $Path->id)->get();
}
$Tasks = $Task[0]->path ?? null;
$AllSubs     = [];
$AllSubTasks = $SubTasks    = null;
if (!empty($Subs)) {
    foreach ($Subs as $sub) {
        $AllSubs[] = $sub->task_id;
    }
    $AllSubTasks = implode(" ", $AllSubs);
    $SubTasks    = explode(",", ($AllSubTasks));
}

答案 1 :(得分:-1)

正确缩进代码后(如正确建议@castis),您可以开始将语句中的位提取到方法中。这就是所谓的重构。

来自wikipedia

  

代码重构是在不更改其外部行为的情况下重构现有计算机代码的过程,即更改因子。

它看起来像这样:

if(true) {
    doWhateverNeedsToBeDone();
} else {
    doTheOtherThing();
}

然后您将所有代码复制并粘贴到这些新方法的主体中。