laravel 加入 select 最大加入列
laravel join select max of joined column
我有 3 个 table products, details 和
detail_colors
我想要 select max "stock" from "detail_colors"
和
max "price" from "details"
$products = Product::
join('details', function (JoinClause $join) {
$join->on('products.id', '=', 'details.product_id');
})
->join('detail_colors', function (JoinClause $join) {
$join->on('products.id', '=', 'detail_colors.product_id');
})
->select('products.*', DB::raw('max(details.price) as price'), DB::raw('max(detail_colors.stock) as stock'))
它不起作用。
我用laravel 8.*
不能对关系使用聚合函数吗?
$products = Product::query()
->withMax('details', 'price')
->withMax('detail_colors', 'stock')
或者您可以这样定义关系:
public function details()
{
return $this->hasOne(Details::class)->ofMany('price', 'max');
}
public function details()
{
return $this->hasOne(DetailColors::class)->ofMany('stock', 'max');
}
我有 3 个 table products, details 和 detail_colors
我想要 select max "stock" from "detail_colors"
和
max "price" from "details"
$products = Product::
join('details', function (JoinClause $join) {
$join->on('products.id', '=', 'details.product_id');
})
->join('detail_colors', function (JoinClause $join) {
$join->on('products.id', '=', 'detail_colors.product_id');
})
->select('products.*', DB::raw('max(details.price) as price'), DB::raw('max(detail_colors.stock) as stock'))
它不起作用。
我用laravel 8.*
不能对关系使用聚合函数吗?
$products = Product::query()
->withMax('details', 'price')
->withMax('detail_colors', 'stock')
或者您可以这样定义关系:
public function details()
{
return $this->hasOne(Details::class)->ofMany('price', 'max');
}
public function details()
{
return $this->hasOne(DetailColors::class)->ofMany('stock', 'max');
}