如何在 laravel eloquent 查询中使用数学函数?
How can I use math function in laravel eloquent query?
我想在我的 eloquent 查询中进行求和数学计算。我有一个变量,它必须是我的某些数据库字段的多个变量,如果超过 3000,则父项必须在我的结果中。
1)宝石型号:
class Jewel extends Model
{
use HasFactory;
public function jewelsItems(){
return $this->hasMany('App\Models\JewelsItem');
}
}
2)饰品型号:
class JewelsItem extends Model
{
//has weight,fee field
use HasFactory;
protected $table = 'jewel_items';
public function jewel(){
return $this->belongsTo('App\Models\Jewel');
}
}
查询必须为我提供至少具有 (weight*($varibale+fee)) > 5000
.
的 jewelsItem 的所有珠宝
note1: $varibale 不是数据库字段。
note2: 我不想使用 Raw Expressions 因为他们的漏洞。
您可能想试试这个:
return Jewel::whereHas('jewelsItems', function ($query) use ($variable) {
$query->whereRaw("(weight*({$varibale}+fee)) > 5000");
})->get();
我想在我的 eloquent 查询中进行求和数学计算。我有一个变量,它必须是我的某些数据库字段的多个变量,如果超过 3000,则父项必须在我的结果中。
1)宝石型号:
class Jewel extends Model
{
use HasFactory;
public function jewelsItems(){
return $this->hasMany('App\Models\JewelsItem');
}
}
2)饰品型号:
class JewelsItem extends Model
{
//has weight,fee field
use HasFactory;
protected $table = 'jewel_items';
public function jewel(){
return $this->belongsTo('App\Models\Jewel');
}
}
查询必须为我提供至少具有 (weight*($varibale+fee)) > 5000
.
note1: $varibale 不是数据库字段。
note2: 我不想使用 Raw Expressions 因为他们的漏洞。
您可能想试试这个:
return Jewel::whereHas('jewelsItems', function ($query) use ($variable) {
$query->whereRaw("(weight*({$varibale}+fee)) > 5000");
})->get();