项目过滤 laravel eloquent

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38272593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:07:20  来源:igfitidea点击:

Items filter laravel eloquent

phplaraveleloquent

提问by aGoodRussianGuy

What is the best way to implement the items filter functionality?

实现项目过滤器功能的最佳方法是什么?

I have a table with some items, and each item has some fields.

我有一个包含一些项目的表格,每个项目都有一些字段。

How can I select items with fields filtering, like e-commerce filtering, using Laravel 5 and Eloquent?

如何使用 Laravel 5 和 Eloquent 选择带有字段过滤的项目,例如电子商务过滤?

回答by Steve Bauman

I've always used eloquent scopes to filter eloquent results: https://laravel.com/docs/5.1/eloquent#query-scopes

我一直使用 eloquent scopes 来过滤 eloquent 结果:https: //laravel.com/docs/5.1/eloquent#query-scopes

Your Controller:

您的控制器:

use App\Item;

class ItemController extends Controller
{
    public function index(Request $request)
    {
        $items = Item::name($request->name)->price($request->price)->paginate(25);

        return view('items.index', compact('items'));
    }
}

Your Model:

您的型号:

class Item extends Model
{
    public function scopeName($query, $name)
    {
        if (!is_null($name)) {
            return $query->where('name', 'like', '%'.$name.'%');
        }

        return $query;
    }

    public function scopePrice($query, $price)
    {
        if (!is_null($price)) {
            return $query->where(compact('price'));
        }

        return $query;
    }
}

Your View:

您的观点:

<form action="{{ route('items.index') }}" method="get">
    <input type="text" name="price" />
    <input type="text" name="name" />
    <input type="submit" value="Search">
</form>

@foreach($items as $item)
    {{ $item->name }}
@endforeach

{!! $items->render() !!}

In your scopes, you can check if the given value is null before limiting your query results. This effectively allows users to search and filter paginated results.

在您的范围内,您可以在限制查询结果之前检查给定值是否为空。这有效地允许用户搜索和过滤分页结果。

You could further enhance the search by automatic validation using Laravel's Form Requestsso you can sure that the input they are searching for matches the data type for your query.

您可以使用 Laravel 的表单请求通过自动验证来进一步增强搜索,这样您就可以确保他们正在搜索的输入与您的查询的数据类型相匹配。

Keep in mind you'll have to modify the getRedirectUrl()method since your users will be executing a GETrequest and by default the getRedirectUrl()will contain the GETURL parameters resulting in an infinite loop since the one of the params failed validation.

请记住,您必须修改该getRedirectUrl()方法,因为您的用户将执行GET请求,并且默认情况下该方法getRedirectUrl()将包含GETURL 参数,从而导致无限循环,因为其中一个参数验证失败。

Here's an example using the above:

这是使用上述内容的示例:

class ItemSearchRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'name' => 'nullable|string|min:2|max:100',
            'price' => 'nullable|numeric',
        ];
    }

    protected function getRedirectUrl()
    {
        return url('/items');
    }
}

回答by Eric Tucker

I wrote a package for exactly this here: https://github.com/Tucker-Eric/EloquentFilter

我在这里写了一个包:https: //github.com/Tucker-Eric/EloquentFilter

It allows you to do something like:

它允许您执行以下操作:

use App\Item;

class ItemController extends Controller
{
    public function index(Request $request)
    {
        $items = Item::filter($request->all())->paginateFilter(25);

        return view('items.index', compact('items'));
    }
}