首页 > 解决方案 > 如何在laravel中搜索2个日期的特定一条记录?

问题描述

在我的请假请求页面中,我在请假期限列上有一个搜索栏,在请假期限列中有日期,第一个是请假开始日期,第二个是请假结束日期。

我想这样做,在搜索栏中,如果我在迄今为止的日期之间写一个日期,而不是得到该记录的输出。我的数据库是 mongodb

leavedate 是可变的,我正在查找记录。

这是我的控制器文件代码

 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>

标签: phplaravelmongodbeloquent

解决方案


尝试替换函数的代码块:

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'));
    }
}

让我知道它是否有效?


推荐阅读