簡體   English   中英

如何從Laravel中的另一個相關表中獲取mysql列的最大值和最小值

[英]How to get max and min of mysql column from another related table in Laravel

這是我的數據庫表

  • 清單
  • 要求
  • 產品展示

現在每個請求都有一個產品,每個列表都有許多請求,我現在想獲取列表X中產品的最小和最大價格/尺寸,如何使用Laravel Elequent實現呢?

列表表

+----+----------+
| id | user_id  |
+====+==========+
| 1  | 1        |
+----+----------+
| 2  | 1        |
+----+----------+
| 3  | 2        |
+----+----------+

要求表

+----+-------------+----------+
| id | product_id  | list_id  |
+====+=============+==========+
| 1  | 1           | 1        |
+----+-------------+----------+
| 2  | 1           | 1        |
+----+-------------+----------+
| 3  | 2           | 2        |
+----+-------------+----------+

產品表

+----+-------+------+-------+
| id | price | size | other |
+====+=======+======+=======+
| 1  | 110   | 10   | test  |
+----+-------+------+-------+
| 2  | 130   | 12   | test  |
+----+-------+------+-------+
| 3  | 100   | 24   | test  |
+----+-------+------+-------+
| 4  | 200   | 16   | test  |
+----+-------+------+-------+

我想獲取清單X中產品的最小和最大價格/尺寸

$maxSize = Product::whereHas('request.list', function($q) use($listId) {
    $q->where('list.id', $listId);
})
->max('size');

$minSize = Product::whereHas('request.list', function($q) use($listId) {
    $q->where('list.id', $listId);
})
->min('size');

對最低和最高價格執行相同的操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM