【问题标题】:how to get record from database within multiple conditions of between max and min value如何在最大值和最小值之间的多个条件下从数据库中获取记录
【发布时间】:2022-09-23 04:10:27
【问题描述】:

我有一个包含产品的表,我想自动定价。 我尝试创建一个名为 price 的表。 价格表的结构如下:

  • 最大高度
  • 最大宽度
  • max_long
  • 最大重量
  • min_height
  • 最小宽度
  • min_long
  • min_weight
  • 价格

我想根据产品的(高-宽-长-重)来检索价格

我试过这种方式:

  • 从控制器:
<?php

namespace App\\Http\\Controllers;


use App\\Models\\Coli;
use App\\Models\\Pricing;
use Illuminate\\Pipeline\\Pipeline;


class ColiPriceController extends Controller
{



    public static $data = [];
 public static function price($id){
    
       ColiPriceController::setData($id);

        $price = app(Pipeline::class)
            ->send(Pricing::query())
            ->through([
               
                \\App\\Filters\\Pricing\\MaxHeightPriceFilter::class,
                \\App\\Filters\\Pricing\\MinHeightPriceFilter::class,
                
                \\App\\Filters\\Pricing\\MaxLongPriceFilter::class,
                \\App\\Filters\\Pricing\\MinLongPriceFilter::class,
                
                \\App\\Filters\\Pricing\\MaxWidthPriceFilter::class,
                \\App\\Filters\\Pricing\\MinwidthPriceFilter::class,
                
                \\App\\Filters\\Pricing\\MaxWeightPriceFilter::class,
                \\App\\Filters\\Pricing\\MinWeightPriceFilter::class,
        
                ])
            ->thenReturn()
            
            ->first();
}

protected static  function setData($id)
    {
        $coli = Coli::find($id);

        $coli->height = ($coli->height) ? intval($coli->height) : 0;
        $coli->width = ($coli->width) ? intval($coli->width) : 0;
        $coli->longeur = ($coli->longeur) ? intval($coli->longeur) : 0;
        $coli->wieght = ($coli->wieght) ? intval($coli->wieght) : 0;

        $data = [
            \'height\'    => $coli->height,
            \'width\'     => $coli->width,
            \'long\'      => $coli->longeur,
            \'weight\'     => $coli->wieght,
        ];
       
        return ColiPriceController::$data = $data;

    }
}

从 MaxHeightFilter :


<?php

namespace App\\Filters\\Pricing;

use Closure;

class MaxHeightPriceFilter extends PriceFilter
{
    public $column = \"max_height\";
    public $dataColumn = \"height\";
    public $operator = \"<=\";

    public function handle($request, Closure $next)
    {
      
        return $this->filter($request, $next);
    }

    
}



来自 PriceFilter :


<?php

namespace App\\Filters\\Pricing;

use Illuminate\\Database\\Eloquent\\Builder;
use App\\Http\\Controllers\\ColiPriceController;


class PriceFilter 
{

    public $column = \"max_weight\";
    public $dataColumn = \"weight\";
    public $operator = \"<=\";

    
    protected  function filter($request, $next)
    {
       
        if($this->chequePriceToContinue($request)){
         
            return $next(static::removeWhere($request, $this->column));
           }
           
        return $next($request);
        
        // return $next($request->where($this->column, $this->operator, \':\'.ColiPriceController::$data[$this->dataColumn]));
       
    }

    public function chequePriceToContinue($request){
        
        $price = $request->where($this->column, $this->operator,  ColiPriceController::$data[$this->dataColumn] )->get();
        if(is_array($price)){
            return true;
        }
        
        return false;
    }


     /**
     * @param Builder $builder
     * @param $whereColumn
     * @return Builder
     */
    public static function removeWhere(Builder $builder, $whereColumn)
    {
        $bindings = $builder->getQuery()->bindings[\'where\'];
        $wheres = $builder->getQuery()->wheres;

        $whereKey = false;
        foreach ($wheres as $key => $where) {
            if ($where[\'column\'] == $whereColumn) {
                $whereKey = $key;
                break;
            }
        }

        if ($whereKey !== false) {
            unset($bindings[$whereKey]);
            unset($wheres[$whereKey]);
        }

        $builder->getQuery()->wheres = $wheres;
        $builder->getQuery()->bindings[\'where\'] = $bindings;

        return $builder;
    }


    
}
  • 只是为了理解这里的思路:您能解释一下为什么在控制器中使用静态函数以及为什么要这样抓取管道吗?我在 \'{projectRoot}/bootstrap/app.php\' 文件中看到过这样的代码,但我想知道你为什么要在控制器中构建它?在我看来,这可以更容易地完成,或者我错过了有关此处所做选择的一些背景信息。
  • 我在调用控制器外部的函数时使用静态函数很容易,我使用管道过滤数据直到我得到我正在寻找的东西,我使用这个控制器是因为我需要在另一个资源中处理这些数据,怎么可能正如你所说,我以最简单的方式完成这项工作?

标签: php sql laravel database eloquent


【解决方案1】:

好的,根据您的信息,我认为您的解决方案应如下所示:

首先,如果您还没有 Price 模型,请确保您拥有它。 其次,使用您的模型在控制器中构建查询,如下所示:

//ColiPriceController.php

function findPrices(Request $request) {
    //If id is provided, take coli from db
    if($request->has('id')) {
        //FindOrFail returns 404 if not found
        $coli = Coli::findOrFail($request->id);
    }
    //Start query.
    $query = Price::query();
    //Set values based on coli if set OR by request parameters
    $height = $coli->height ?? $request->height;
    $width = $coli->width ?? $request->width;
    $long = $coli->long ?? $request->long;
    $weight = $coli->weight ?? $request->weight;

    //If the initialized height/width/long/weight is greater than 0, add the where clauses to query
    if($height > 0) {
        $query
            ->where('max_height', '>', $height)
            ->where('min_height', '<', $height);
    }
    if($width > 0) {
        $query
            ->where('max_width', '>', $width)
            ->where('min_width', '<', $width);
    }
    if($long > 0) {
        $query
            ->where('max_long', '>', $long)
            ->where('min_long', '<', $long);
    }
    if($weight > 0) {
        $query
            ->where('max_weight', '>', $weight)
            ->where('min_weight', '<', $weight);
    }
    //I'm using get() and not first() because im not sure if you can guarantee you only find 1 result and not multiple.
    //If you are confident you only get 1 result per query, feel free to make it first()
    return $query->get();
}

所以在这里我只添加 where 子句,如果你初始化了相应的变量。这不是强制性的,但从我的角度来看似乎是合乎逻辑的。 findPrices 函数应该由路由调用。您可以将idheightwidthlongweight 作为请求参数传递。如果设置了id 并找到了大肠杆菌,则忽略其他参数。

希望对你有帮助,欢迎反馈

【讨论】:

    猜你喜欢
    • 2019-05-04
    • 1970-01-01
    • 2010-11-06
    • 2018-11-15
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多