php - Laravel 雄辩的构建查询
问题描述
我想使用 Laravel Eloquent 结构编写一个查询。我想在输入最小值和最大值后查询这些值。我有一个产品表。带有折扣 (%) 和价格的列产品表。我想根据产品的折扣值获得最小值和最大值。
我希望这是基于包括折扣在内的查询。
$products->where('price', '<=', $max)
->where('price', '>=', $min)
->orderByDesc('id')
->paginate(9);
产品迁移:
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('upper_category_id')->index();
$table->unsignedBigInteger('category_id')->index();
$table->unsignedBigInteger('user_id')->index();
$table->string('name');
$table->longText('text');
$table->float('price')->index();
$table->integer('stock');
$table->float('discount');
$table->timestamps();
});
非常感谢您提前。
解决方案
如果我正确理解了您的问题,您可以尝试这样做:
$products->where(DB::raw('price - price * discount'), '<=', $max)
->where(DB::raw('price - price * discount'), '>=', $min)
->orderByDesc('id')
->paginate(9);
推荐阅读
- java - Avatica Calcite JDBC 驱动程序问题
- autosave - 有没有办法在 Visual Studio 2017 中禁用自动保存?
- angular - Angular 4+ 有条件地渲染 mat-menu(所有或部分 mat 菜单选项)
- javascript - make the icon of the first open accordian in each section start with "-", right now they all start with "+"
- php - 如何在 PHP 中使用不同的数据更新 2 个不同的列?
- windows - Apache Ant:使用通配符解析路径到 FileSet
- javascript - can't use javascript function in php
- javascript - 如何减少 Node JS 中多个异步函数的运行时间?
- c# - EF Add-Migration 命令快照提供 Redendant 架构更改
- mysql - Centos 7 , WHM 高 Mysql&MariaDB CPU 使用率 %600