如何对产品进行过滤(laravel)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48157824/
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 17:13:24  来源:igfitidea点击:

How to do filter for products (laravel)

phplaravel

提问by Alexxosipov

I do the online shop. I have 3 pages where filters for products are similar - Catalog page, parent category page and children category page

我是做网店的。我有 3 个页面,其中产品过滤器相似 - 目录页面、父类别页面和子类别页面

site/catalog/  // catalogController@index
site/catalog/parentCategory/childrenCategory //childrenCategoryController@index
site/catalog/parentCategory //parentCategoryController@index

The filter is made via get requests, like that: site/catalog?price_from=1&price_to=9999&color=red. How to make this filter into a separate function? Will it be good to make this in a product model? And how to use it? I think it should be a function that accepts 2 params (request params and current query model) and returns $this.

该过滤器是通过GET请求提出,这样的:site/catalog?price_from=1&price_to=9999&color=red。如何使这个过滤器成为一个单独的功能?在产品模型中制作它会好吗?以及如何使用它?我认为它应该是一个接受 2 个参数(请求参数和当前查询模型)并返回$this.

Code in controllers:

控制器中的代码:

$products = Product::filter($products, $request)->paginate(20);     

Model:

模型:

public function filter($model, Request $request) {
        if ($request->has('price_from')) {
            $model->where('price', '>', $request->get('price_from'));
        }
        if ($request->has('color')) {
            $model->where('color', '>', $request->get('color'));
        }

        return $model;
    }

But how to do it right? How to pass current $productsin controller code?

但是怎么做才是正确的呢?如何$products在控制器代码中传递电流?

回答by Alexey Mezenin

You can create a local scope. Something like:

您可以创建一个本地范围。就像是:

public function scopeFilter($q)
{
    if (request('price_from')) {
        $q->where('price', '>', request('price_from'));
    }
    if (request('color')) {
        $q->where('color', '>', request('color'));
    }

    return $q;
}

Then use it:

然后使用它:

Product::filter()->paginate(20);

回答by Armin

Had to do something like this recently, here is my whole ProductsController.phpfile: `

最近不得不做这样的事情,这是我的整个ProductsController.php文件:`

         <?php
         namespace App\Http\Controllers;
         use Illuminate\Http\Request;
         use App\Product;

        class ProductsController extends Controller
        {
            public function index(Request $request)
            {
                $products = Product::all();

                if ($request->type) {
                    $products = $products->whereIn('type', $request->type);
                }

                if ($request->gender) {
                  $products = $products->whereIn('gender', $request->gender);
                }

               return $products;

`

`

You can continue adding on and filter as much as needed, worked like a charm for me, note that I used whereIn()as my requested data was coming in as an array, hope this helps.

您可以根据需要继续添加和过滤,这对我来说就像一个魅力,请注意,我使用的whereIn()请求数据是作为数组传入的,希望这会有所帮助。

回答by T T

I have a similiar problem. I want to make function whose use different filter for each location name. Now I have to make two functions for each filter. I'm trying to make something like this:

我有一个类似的问题。我想制作对每个位置名称使用不同过滤器的函数。现在我必须为每个过滤器创建两个函数。我正在尝试做这样的事情:

public function chooseLocation($id)
        {
            $locations = Location::find($id);
            $orders = Order::all();

        if($location_name = NewYork ){
            $locations = $locations->where('location', '=', 'New York')->whereRaw('Date(created_at) = CURDATE()')->get();
        }
        if($locations = NC){
            $locations = $locations->where('location', '=', 'NC')->whereRaw('Date(created_at) = CURDATE()')->get();
        }
    }

I want to make two filters in one function. Not like now.

我想在一个函数中制作两个过滤器。不像现在。

 public function wallet($id)
{
    $locations = Location::find($id);
    $wallets = Wallet::all();
    $orders = Order::all();
    $orders = Order::where('location', '=', 'Plovdiv')->whereRaw('Date(created_at) = CURDATE()')->get();
    return view('wallet')
    ->with('locations', $locations)
    ->with('orders', $orders)
    ->with('wallets', $wallets);
}
public function wallet2($id)
{
    $locations = Location::find($id);
    $wallets = Wallet::all();
    $orders = Order::all();
    $orders = Order::where('location', '=', 'New York')->whereRaw('Date(created_at) = CURDATE()')->get();
    return view('wallet')
    ->with('locations', $locations)
    ->with('orders', $orders)
    ->with('wallets', $wallets);
}