laravel - 使用预加载将原始 SQL 查询转换为 Laravel Eloquent
问题描述
我在尝试将以下查询转换为 Eloquent 时遇到了很大的困难。
SELECT Sum(t.amount) AS amount,
m.name
FROM transactionsv2 t
JOIN channels c
ON t.entityid = c.uuid
JOIN merchants m
ON c.sender = m.uuid
WHERE t.paymenttype = 'DB'
AND t.status = 1
AND t.processing_time >= '2019-01-01'
AND t.processing_time < '2019-01-21'
GROUP BY m.name;
这就是我所拥有的,但结果集不正确......
public function getTransactionsVolumeReport()
{
$report = Transaction::select(DB::raw('sum(amount) as amount, entityId'))
->where('paymentType', '=', 'DB')
->where('status', 1)
->where('processing_time', '>=', '2019-01-01 00:00:00')
->where('processing_time', '<=', '2019-01-21 23:59:59')
->with(['channel' => function ($q) {
$q->select('uuid', 'name', 'sender');
}])
->with(['channel.merchant' => function ($q) {
$q->select('uuid', 'name')
->groupBy('name');
}])
->get();
echo $report;
}
这些是 Laravel 调试栏显示的查询......
以下是我的雄辩关系...
Transaction Model
protected $with = ['channel', 'statusPayment'];
public function channel() {
return $this->hasOne(Channel::class, 'uuid', 'entityId');
}
-----
Channel Model
protected $with = ['merchant', 'attachedMerchantAccount'];
public function merchant() {
return $this->hasOne('App\Merchant', 'uuid', 'sender');
}
解决方案
推荐阅读
- shell - 是否可以通过 gnu awk 命令使用变量而不是文件?
- php - 如何在 MySQL 中存储 4 位 HOUR 格式
- javascript - 如何将分页元素从 PNG/SVG 转换为 HTML?
- python - 在R中加载泡菜
- oracle - 顶点交互式网格选择列表
- php - 从数据库表中检索数据时出现错误
- python - 如何在 Python 中进行并行处理?
- node.js - 在 Adonisjs 中插入多行不起作用
- java - 使用 brew cask 在 MacOs Catalina10.15 上安装 Java6
- scala - 如何将 Set[Tuple2] 中的数据转换为案例类