什么是更好的方式,而不是在Laravel中使用GOTO

时间:2018-05-18 12:44:45

标签: laravel eloquent goto

现在我必须在Laravel 5.6v中使用GOTO作为一些逻辑。

如何制作与GOTO和更漂亮的代码相同的代码? :)

public function job_finished_search(Request $request)
{
    $rFrom = $request->input('datepicker_from');
    $rTo = $request->input('datepicker_to');
    $rName =$request->customer_name;

    if($rFrom == null){
        if($rTo == null){
            if($rName == null){
            Flash::error('Search result not found');
            return redirect(route('pdf.index'));
            }
            $query = Job::onlyTrashed()
                        ->where('is_trash', 0)
                        ->where('customer_name', $rName)
                        ->orderBy('created_at', 'desc');
                        goto A;
        }

        elseif($rTo != null){
            if($rName == null){
                $query = Job::onlyTrashed()
                ->where('is_trash', 0)
                ->where('created_at', '<=', Carbon::parse($rTo)->endOfDay())
                ->orderBy('created_at', 'desc');
                goto A;
            }
        ...
        ...
}

A: $job_ids = $query->pluck('id')->all();  

1 个答案:

答案 0 :(得分:1)

更清洁的方式,

$buildquery=Job::onlyTrashed()->where('is_trash', 0);

if($request->filled('customer_name')){
    $buildquery->where('customer_name', $request->customer_name);
}

if($request->filled('datepicker_to')){
    $buildquery->where('created_at', '<=', Carbon::parse($request->datepicker_to)->endOfDay());
}

$result = $buildquery->orderBy('created_at', 'desc')->get();

$job_ids = $result->pluck('id');

你没有在任何地方使用$rFrom所以我省略了它。