简体   繁体   English

Laravel 5搜索结果分页

[英]Laravel 5 search results pagination

I have created some search filters using a textbox and drop downs so when I click the search button i want to use any of these filters to query eloquent relationships. 我已经使用文本框和下拉列表创建了一些搜索过滤器,因此当我单击搜索按钮时,我想使用这些过滤器中的任何一个来查询雄辩的关系。

The search works fine and I get the correct amount of results but the problem lies with the pagination. 搜索工作正常,我得到正确数量的结果,但问题出在分页上。 When I click on the next page button (or any other page), the result is result and I get everything again. 当我单击下一页按钮(或任何其他页面)时,结果就是结果,我又得到了一切。

I know this because I'm not doing a request when the paging is clicked so my question is how do i perform so same query but using the paging? 我知道这是因为单击分页时我没有执行请求,所以我的问题是如何执行相同的查询但使用分页?

Here is my controller function 这是我的控制器功能

public function index(Request $request)
{
    if(!empty($request)) {

        $products = Product::with(['supplier', 'carrier', 'name']);

        if($request->name) {
            $products = $products->whereHas('Name', function ($query) use ($request) {
                $products = $query->where('Name', $request->name);
            });
        }
        if($request->subtype) {
            $products = $products->whereHas('SubType', function ($query) use ($request) {
                $products = $query->where('SubTypeId', $request->subtype);
            });
        }
        if($request->supplier) {
            $products = $products->whereHas('Supplier', function ($query) use ($request) {
                $products = $query->where('SupplierId', $request->supplier);
            });
        }
        if($request->carrier) {
            $products = $products->whereHas('Carrier', function ($query) use ($request) {
                $products = $query->where('CarrierId', $request->carrier);
            });
        }

        $products = $products->paginate(8);
    }
    else {
        $products = Product::with(['supplier', 'carrier', 'name'])->paginate(8);
    }

    $suppliers = Supplier::all();
    $carriers = Carrier::all();
    $subtypes = SubType::all();

    $options = array(
        'products' => $products,
        'suppliers' => $suppliers,
        'subtypes' => $subtypes,
        'carriers' => $carriers
    );

    return view('products.index')->with($options);
}

I was able to fix it by changing the controller function. 我可以通过更改控制器功能来修复它。 I didn't the IF statement 我没有IF声明

$products = Product::with(['supplier', 'carrier', 'name']);

if($request->name) {
    $products = $products->whereHas('Name', function ($query) use ($request) {
        $query->where('Name', $request->name);
    });
}
if($request->subtype) {
    $products = $products->whereHas('SubType', function ($query) use ($request) {
        $query->where('SubTypeId', $request->subtype);
    });
}
if($request->supplier) {
    $products = $products->whereHas('Supplier', function ($query) use ($request) {
        $query->where('SupplierId', $request->supplier);
    });
}
if($request->carrier) {
    $products = $products->whereHas('Carrier', function ($query) use ($request) {
        $query->where('CarrierId', $request->carrier);
    });
}

$products = $products->paginate(8);

$suppliers = Supplier::all();
$carriers = Carrier::all();
$subtypes = SubType::all();

$options = array(
    'products' => $products,
    'suppliers' => $suppliers,
    'subtypes' => $subtypes,
    'carriers' => $carriers
);

return view('products.index')->with($options);

In my view where I put the pagination link, I replaced 在我认为放置分页链接的位置,我替换了

{{ $products->links() }}

with

{{ $products->appends(\Request::except('_token'))->render() }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM