如何在Laravel中的2个日期的持续时间内搜索特定的一条记录?

时间:2019-04-22 09:55:48

标签: php laravel mongodb eloquent

在休假请求页面上,休假持续时间列上有一个搜索栏,在休假持续时间列中有日期,第一个是休假的开始日期,第二个是休假的结束日期。

我想这样做,如果我在搜索栏中写了一个日期,但我没有得到该记录的输出。 我的数据库是mongodb

离开日期是可变的,我正在查找记录。

这是我的控制器文件代码

 public function listOfLeave(Request $request)
    {


        $allLeaves = null;
        if (
            !empty($request->input('name')) || 
            !empty($request->input('leaveType')) ||
            !empty($request->input('leaveDate')) ||
            !empty($request->input('appliedDate')) || 
            !empty($request->input('status'))       
        )
        {
            $flg = false; 

            if (!empty($request->input('name')))
            {
                $flg = true; 
                $allLeaves=LeaveManagement::where('name', 'LIKE', "%{$request->input('name')}%");
            }

            if (!empty($request->input('leaveType')))
            {
                if($flg)
                {
                    $allLeaves=$allLeaves->orWhere('type', 'LIKE', "%{$request->input('leaveType')}%");
                }
                else 
                {
                    $allLeaves=LeaveManagement::where('type', 'LIKE', "%{$request->input('leaveType')}%");
                }
            }



            if (!empty($request->input('leaveDate'))) 
            {
                if ($flg) 
                {
                    $allLeaves = $allLeaves->orWhere('end', '>=', $request->input('leaveDate'))
                                            ->orWhere('start', '<=', $request->input('leaveDate'));
                } 
                else 
                {
                    $allLeaves = LeaveManagement::where('end', '>=', $request->input('leaveDate'));
                }
            }

            if (!empty($request->input('appliedDate')))
            {
                if($flg)
                {
                    $allLeaves=$allLeaves->orWhere('start', 'LIKE', "%{$request->input('appliedDate')}%");
                }
                else 
                {
                    $allLeaves=LeaveManagement::where('start', 'LIKE', "%{$request->input('appliedDate')}%");
                }
            }

            if (!empty($request->input('status')))
            {
                if($flg)
                {
                    $allLeaves=$allLeaves->orWhere('status', 'LIKE', "%{$request->input('status')}%");
                }
                else 
                {
                    $allLeaves=LeaveManagement::where('status', 'LIKE', "%{$request->input('status')}%");
                }
            }

            $allLeaves = $allLeaves ->orderBy('name', 'ASC')
                                    ->orderBy('type', 'ASC')
                                    ->orderBy('start', 'ASC')
                                    ->orderBy('start', 'ASC')
                                    ->orderBy('status', 'ASC')
                                    ->paginate(25);

            return view('pages.newleaverequest')->with(['allLeaves'=>$allLeaves]);  
        }
        else 
        {
            $allLeaves=LeaveManagement::orderBy('name', 'ASC')->paginate(25);  
        }

        $allLeaves->appends(array('name'=>Input::get('name')));

        return view('pages.newleaverequest', compact('allLeaves'));

    }

这是我的查看文件

<form action="{{ route('manage_leave') }}" method="GET" role="search">
<div class="row">
  <div class="form-group">

  <input type="text" class="form-control " id="search1" name="name" placeholder="Employee Name">                                    
  <select class="custom-select form-control col-md-2" id="search5" name="leaveType">
     <option value="">Leave Type</option>
     <option value="casual">Casual Leave</option>
     <option value="medical">Medical Leave</option>
     <option value="early">Early Leave</option>
</select>

   <input type="date" class="form-control " id="search3" name="leaveDate" placeholder="Leave Date">
   <input type="date" class="form-control " id="search4" name="appliedDate" placeholder="Applied Date">
   <select class="custom-select form-control col-md-2" id="search5" name="status">
   <option value="">Status</option>
   <option value="Pending">Pending</option>
   <option value="Approved">Approved</option>
   <option value="Rejected">Rejected</option>
</select>
&emsp;&emsp;
<button class="btn btn-info" type="submit"><i class="fa fa-search "></i></button>
</div>
</div>
</form>
  @if(isset($allLeaves))
                        <table class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th>Employee Name</th>
                                    <th>Leave Type</th>
                                    <th>Leave Duration</th>
                                    <th>Applied On</th>
                                    <th>Status</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                @if($allLeaves != null)
                                @foreach($allLeaves as $leave)
                                <tr>
                                    <td> {{ $leave->name }} </td>
                                    <td> {{ $leave->type }} </td>
                                    <td>
                                        {{ date('d-m-Y', strtotime($leave->start)) }} To
                                        {{ date('d-m-Y', strtotime($leave->end)) }}
                                    </td>
                                    <td> {{ date('d-m-Y', strtotime($leave->start)) }} </td>
                                    <td>
                                        @if($leave['status']=='Pending')
                                        <span class="badge badge-pill badge-warning" style="color:white;font-size: 90%;font-weight:bold;">Pending</span>
                                        @elseif($leave['status']=='Approved')
                                        <span class="badge badge-pill badge-success" style="color:white;font-size: 90%;font-weight:bold;">Approved</span>
                                        @else
                                        <span class="badge badge-pill badge-danger" style="color:white;font-size: 90%;font-weight:bold;">Rejected</span>
                                        @endif
                                    </td>
                                    <td>
                                        <a href="{{route('handle_leave', $leave->id)}}" class="btn btn-info text-white">Details</a>
                                    </td>
                                </tr>
                                @endforeach
                                @endif
                            </tbody>
                        </table>
                        <div>
                            {{ $allLeaves->render() }}
                            @endif
                        </div>
                    </div>
                </div>
            </div>

1 个答案:

答案 0 :(得分:0)

尝试替换函数的代码块:

if (!empty($request->input('leaveDate'))) 
{
    if ($flg) 
    {
        $allLeaves = $allLeaves->orWhere(function($query) use ($request){
            $query->whereDate('end', '>=', $request->input('leaveDate'))
                ->whereDate('start', '<=', $request->input('leaveDate'));
        });
    } 
    else 
    {
        $allLeaves = LeaveManagement::where('end', '>=', $request->input('leaveDate'));
    }
}

让我知道它是否有效吗?