我如何在Laravel中从HTTP请求中获取记录?

时间:2019-04-18 04:46:26

标签: php laravel mongodb

我的listDepartment函数的工作方式类似于从数据库中获取我的所有记录并显示我,我也放置了一个搜索栏,但是在实现搜索条形码后,它没有显示任何记录。

这是我的控制器文件

public function listDepartment(Request $request)
    {
        $listOfDepartment = $request->input('search');
        $listOfDepartment=Department::where('nameOfDepartment', 'LIKE', "%{$request->input('search')}%")
            ->orwhere('createdBy', 'LIKE', "%{$request->input('search')}%")->orderBy('nameOfDepartment', 'ASC')->paginate(2);  

        $listOfDepartment->appends (array('search'=>Input::get('search')));

        return view('pages.department', compact('listOfDepartment'));
    }

我希望如果我不搜索任何内容,它将显示我部门收藏中的所有记录 有人可以帮忙吗???

如果我有多个这样的字段怎么办

public function listOfLeave(Request $request)
    {
        $search = [$request->input('name'), $request->input('status'),];

        $allLeaves = null;
        if (!empty($search))
        {
            $allLeaves=LeaveManagement::where('username', 'LIKE', "%{$request->input('name')}%")->orderBy('username', 'ASC')  
                                    ->orWhere('typeOfLeave', 'LIKE', "%{$request->input('leaveType')}%")->orderBy('typeOfLeave', 'ASC')
                                    ->orWhere('typeOfLeave', 'LIKE', "%{$request->input('leaveType')}%")->orderBy('typeOfLeave', 'ASC')
                                    ->orWhere('startDate', 'LIKE', "%{$request->input('appliedDate')}%")->orderBy('startDate', 'ASC')
                                    ->orWhere('status', 'LIKE', "%{$request->input('status')}%")->orderBy('status', 'ASC')->paginate(2);

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

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

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

1 个答案:

答案 0 :(得分:1)

尝试:

public function listDepartment(Request $request)
{
    $search = $request->input('search');
    $listOfDepartment = null;
    if (!empty($search){
        $listOfDepartment=Department::where('nameOfDepartment', 'LIKE', "%{$request->input('search')}%")
        ->orwhere('createdBy', 'LIKE', "%{$request->input('search')}%")->orderBy('nameOfDepartment', 'ASC')->paginate(2);  
    }
    else {
        $listOfDepartment=Department::orderBy('nameOfDepartment', 'ASC')->paginate(2);  
    }


    $listOfDepartment->appends(array('search'=>Input::get('search')));

    return view('pages.department', compact('listOfDepartment'));
}

请用您编写的实际代码替换语句的paginate部分。

对于多个字段:

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

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

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

            if (!empty($request->input('appliedDate'))){
                if($flg){
                    $allLeaves=$allLeaves->orWhere('appliedDate', 'LIKE', "%{$request->input('appliedDate')}%");
                }
                else {
                    $allLeaves=LeaveManagement::where('appliedDate', '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('username', 'ASC')
                                    ->orderBy('typeOfLeave', 'ASC')
                                    ->orderBy('appliedDate', 'ASC')
                                    ->orderBy('status', 'ASC')
                                    ->paginate(2);

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

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

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