Heim  >  Fragen und Antworten  >  Hauptteil

Laravel sortiert nach Preis mit besonderen Preiskonditionen

Ich habe eine Tabelle mit den folgenden Spalten (Preis, Sonderangebot, ist es im Angebot).

+-------+------+-----------------+--------------+---------+
| id    | price |  special_price  | is_special   | qty      |
+-------+-------------------------+--------------+----------+
| 1     | 100   |    null         | 0            |  5       |
| 2     | 120   |    99           | 1            |  0       |
| 3     | 300   |    null         | 0            |  1       |
| 4     | 400   |    350          | 1            |  10      |
| 5     | 75    |    69           | 1            |  0       |
| 6     | 145   |    135          | 0            |  1       |
+-------+-------+-----------------+--------------+---------+

Ich möchte die Produkte nach „Preis“ sortieren lassen und wenn die Spalte „is_special“ wahr ist, wählen Sie die Spalte „special_price“ aus.

Ich möchte die folgenden Ergebnisse.

+-------+-----------+-----------------+--------------+--------------+
| id    | price     |  special_price  | is_special   | qty          |
+-------+-----------------------------+--------------+--------------+
| 5     | 75        |    69           | 1            |  0           |
| 2     | 120       |    99           | 1            |  0           |
| 1     | 100       |    null         | 0            |  5           |
| 6     | 145       |    135          | 0            |  1           |
| 3     | 300       |    null         | 0            |  1           |
| 4     | 400       |    350          | 1            |  10          |
+-------+-----------+-----------------+--------------+--------------+

In Roh-SQL sieht es so aus:

SELECT *
FROM products
ORDER BY IF(is_special=0, price, special_price ) ASC;

Ich verwende Laravel und möchte den Abfrage-Builder sortieren und die Ergebnisse erhalten.

Zum Beispiel habe ich dies mit virtuellen Eigenschaften gemacht

/**
 * 获取当前价格
 *
 * @return mixed
 */
 public function getCurrentPriceAttribute()
 {
     return $this->is_special ? $this->special_price : $this->price;
 }

Dann wird die Sammlung sortiert$products->sortBy('current_price'), aber dieses Mal möchte ich den Abfrage-Builder in den Ergebnissen haben. Query Builder kann keine virtuellen Eigenschaften verwenden.

Ich habe die Mehrfachsortierung nach zwei Spalten „Preis“ und „Menge“ versucht

$query = Product::query();

$query->orderByRaw("if(is_special=0, price, special_price) " . request('price', 'ASC'));
$query->orderBy('qty', request('qty', 'DESC'));

$query->get();

Ich habe zwei Filter: „Menge“ und „Preis“.

Bei dieser Mehrfachsortierung möchte ich die Produkte nach Preis und dann alle Produkte nach „Menge“ sortieren. Produkte mit Menge == 0 müssen allen Produkten mit Menge > 0 folgen.

Bitte hilf mir.

P粉030479054P粉030479054363 Tage vor1055

Antworte allen(1)Ich werde antworten

  • P粉207969787

    P粉2079697872023-09-23 10:17:23

    第一个问题

    查询构建器没有访问器,你需要将其选择出来:

    DB::table('products')
       ->select('*')
       ->addSelect(DB::raw('IF(is_special=0, price, special_price ) AS current_price'))
       ->orderBy('current_price')
       ->get();
    

    PS:建议在数据库中进行排序,考虑一下如果在产品上有paginate,它将只对返回的页面数据进行排序。


    第二个问题:

    1. qty > 0 AS 1,qty = 0 AS 0,然后按降序排序:

    2. 按请求的price排序

    3. 按请求的qty排序

    所以产品将把qty > 0放在qty = 0之前,然后将qty > 0的记录按照价格排序,然后所有产品按照qty排序;qty = 0的记录将按照价格排序,然后所有产品也按照qty排序:

    $query = Product::query();
    $query->orderBy(DB::raw(IF('qty > 0, 1, 0')), 'DESC');
    $query->orderBy(DB::raw("IF(is_special=0, price, special_price)"), request('price', 'ASC'));
    $query->orderBy('qty', request('qty', 'DESC'));
    $query->get();
    

    PS:orderByRaw("if(is_special=0, price, special_price) " . request('price', 'ASC')容易受到SQL注入的攻击。改为orderBy(DB::raw("IF(is_special=0, price, special_price)"), request('price', 'ASC'))

    Antwort
    0
  • StornierenAntwort