簡體   English   中英

Adonis Lucid ORM:如何將日期時間更改為僅日期,然后使用.distinct()

[英]Adonis Lucid ORM: How do I change the datetime to a date only and then use the .distinct()

我有一張名為 Orders 的表

這是該表的示例值:

[
  {
     id: 1,
     delivery_date: 2020-06-01 00:00:00,
     quantity: 2
  },
  {
     id: 2,
     delivery_date: 2020-06-01 01:00:00,
     quantity: 2
  },
]

我現在問這個

...
await Order
      .query()
      .select('delivery_date')
      .distinct('delivery_date')
      .fetch()

The.distinct() 不起作用,因為時間不一樣,所以我的問題是即使時間不一樣並且日期相同,我如何使用 distinct 來工作? 我也只需要返回日期,沒有時間

所以我所做的只是將日期和時間分開。 根據項目要求,我認為這對我來說是最好的解決方案,不知道為什么,但阿多尼斯仍在增加我約會的時間,我認為這是幕后的時刻,所以要解決這個問題;

您必須先在 model 上添加此列,讓 Adonis 知道您的列是日期,然后在該數組中添加列名:

static get dates () {
    return super.dates.concat(['delivery_date'])
}

之后,我們需要在使用castDate顯示數據時格式化該列

static castDates(field, value) {
    if (field === 'delivery_date') {
      return value.format('YYYY-MM-DD')
    }

    return super.formatDates(field, value)
}

另請注意,您必須調用.fetch()才能使其工作

所有這些都在文檔中: https://adonisjs.com/docs/4.1/lucid#_dates

暫無
暫無

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

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