Laravel ORM 中的 withSum withAvg, withMax,withMin 的实现

Spoony 53.45m2022-04-261189 次点击
```
Orm::withCount(['relation as relation_sum' =>function($query){‌‌‌‌
$query->select(DB::raw("sum(amount) as relationsum"));
}])
```

## groupBy 对查询结果进行分组出现问题
找到 config/database​.php 在 mysql 下面把 'strict' => true 改为 false。

## where in 数组
```
$Str = implode(',', $v);
$_where[] = [DB::raw("字段名 in ({‌‌$Str})"),'1'];
```
```
$where['status'] = 1;
$ids = [1, 2];
$where[] = [
function ($query) use ($ids) {‌
$query->whereIn('id', $ids);
},
];

$list = User::where($where)->get();
```
```
select * from `users` where (`status` = 1 and (`id` in (1, 2)))
```
收藏 ♥ 感谢
暂无回复

登录注册 后可回复。



GitHub