如何在 laravel eloquent 中组合这些多个查询

Posted

技术标签:

【中文标题】如何在 laravel eloquent 中组合这些多个查询【英文标题】:How to combine these multiple queries in laravel eloquent 【发布时间】:2022-01-19 09:32:37 【问题描述】:

我有以下查询,我想要的最终结果是 $rate

// get latest effective date
    $effectiveDate = CpfEffectiveDate::where('effective_from', '<=', $currentDate)
            ->orderBy("effective_from", 'DESC')
            ->first();

// get scheme related to the effective date and citizenship type
    $scheme = CpfScheme::where("cpf_citizenship_id", $request->cpf_citizenship_id)
        ->where('cpf_effective_date_id', $effectiveDate->id)
        ->first();

// get rate based on scheme and other data
    $rate = CpfRate::where("cpf_scheme_id", $scheme->id)
            ->where("minimum_wage", '<', ceil($totalWage)) // query does not accept floats. should be acceptable as wage tiers should be integers
            ->where("minimum_age", '<', $request->employee_age)
            ->orderBy('minimum_wage', 'DESC')
            ->orderBy('minimum_age', 'DESC')
            ->first();

如何将所有 3 个查询合并为一个?

首先我从第一个表中得到正确的生效日期,然后我用它来找到正确的方案(连同一个Citizenship_id),我用它来找到正确的费率。

以下是以下型号:

CpfRate

  class CpfRate extends Model
  
    protected $table = "cpf_rates";

    protected $primaryKey = "id";

    protected $hidden = ["created_at", "updated_at"];

    public function scheme()
    
        return $this->belongsTo(CpfScheme::class, "cpf_scheme_id");
    

    protected $fillable = [
        "minimum_age",
        "minimum_wage",
        "employer_percentage",
        "employee_percentage",
        "employee_offset_amount", // used for special cases, such as -500 for percentage = 0.15 * (TW - 500)
        "ordinary_wage_cap", // ordinary wage cap
    ];
  

CpfScheme

class CpfScheme extends Model

    protected $table = "cpf_schemes";

    protected $primaryKey = "id";

    protected $hidden = ["created_at", "updated_at"];

    public function citizenship()
    
        return $this->belongsTo(CpfCitizenship::class, "cpf_citizenship_id");
    

    public function effectiveDate()
    
        return $this->belongsTo(CpfEffectiveDate::class, "cpf_effective_date_id");
    

CpfEffectiveDate

class CpfEffectiveDate extends Model

    protected $table = "cpf_effective_dates";

    protected $primaryKey = "id";

    protected $hidden = ["created_at", "updated_at"];

    // mutated to dates
    protected $dates = ['effective_from'];

    public function schemes() 
        return $this->hasMany(CpfScheme::class, "cpf_effective_date_id");
    

CpfCitizenship

class CpfCitizenship extends Model

    protected $table = "cpf_citizenships";

    protected $primaryKey = "id";

    protected $hidden = ["created_at", "updated_at"];

    // fields
    protected $fillable = ['description'];

    public function schemes() 
        return $this->hasMany(CpfScheme::class, "cpf_citizenship_id");
    

【问题讨论】:

【参考方案1】:
$rate = CpfRate::select('cpf_rates.*')
    ->where('cpf_scheme_id', '=', function ($query) use ($request) 
        $query->select('id')
                ->from('cpf_schemes')
                ->where("cpf_citizenship_id", $request->cpf_citizenship_id)
                ->where('cpf_effective_date_id', '=', function($query, $currentDate) 
                    $query->select('id')
                            ->from('cpf_effective_dates')
                            ->where('effective_from', '<=', $currentDate)
                            ->orderBy("effective_from", 'DESC')
                            ->limit(1);
                )
                ->first();
    )
    ->where("minimum_wage", '<', ceil($totalWage)) // query does not accept floats. should be acceptable as wage tiers should be integers
    ->where("minimum_age", '<', $request->employee_age)
    ->orderBy('minimum_wage', 'DESC')
    ->orderBy('minimum_age', 'DESC')
    ->first();

我没有测试,但这里唯一可能有问题的是 $currentdate,所以如果有问题,只需使用 Carbon 类直接在查询中获取当前日期。

【讨论】:

正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。 感谢您的回答!我明天测试一下... 它有效。但是我没有以这种方式传入 $currentDate 。相反,我将它放在两个嵌套函数的 use() 下。此外,我没有测试您的版本,但我为函数参数使用了不同的名称,$query1 和 $query2。谢谢 很高兴能帮到你,如果你是 laravel 和 php 开发者,我们可以在linkedin 或 twitter 上联系。为未来的帮助连接。

以上是关于如何在 laravel eloquent 中组合这些多个查询的主要内容,如果未能解决你的问题,请参考以下文章

如何在 laravel 的 eloquent 中转换表列数据?

如何在Laravel 4中使用Eloquent Model增加列

laravel Eloquent ORM - 如何获得编译查询?

Laravel 4:如何将 WHERE 条件应用于 Eloquent 类的所有查询?

如何在 Laravel/Eloquent 中获得完整的关系

如何使用 eloquent 在 laravel 中编写多个连接