比较Laravel 5中的两列

时间:2016-09-06 12:02:47

标签: php sql laravel laravel-5 eloquent

我正在使用Laravel

比方说,我的表中有两个日期字段。如果我想比较它们,我可以做whereRaw子句。

{
  "name": "[variables('siteName')]",
  "type": "Microsoft.Web/sites",
  "location": "[resourceGroup().location]",
  "apiVersion": "2015-08-01",
  "dependsOn": [
    "[concat('Microsoft.Web/serverfarms/', variables('serverfarm'))]",
    "[resourceid('Microsoft.EventHub/namespaces', variables('fullEventHubNameSpace'))]"
  ],
  "tags": {
    "[concat('hidden-related:', resourceGroup().id, '/providers/Microsoft.Web/serverfarms/', parameters('siteName'))]": "Resource"
  },
  "properties": {
    "name": "[variables('siteName')]",
    "serverFarmId": "[resourceId('Microsoft.Web/serverfarms/', variables('serverfarm'))]",
    "siteConfig": {
      "AlwaysOn": true
    }
  },
  "resources": [
    {
      "name": "appsettings",
      "type": "config",
      "apiVersion": "2015-08-01",
      "dependsOn": [
        "[concat('Microsoft.Web/sites/', variables('siteName'))]",
        "[concat('Microsoft.Insights/components/', variables('appInsightsSiteName'))]",
        "[concat('Microsoft.Web/sites/', variables('otherSiteName'))]",
        "[concat('Microsoft.DocumentDb/databaseAccounts/',variables('databaseAccountName'))]",
        "[resourceid('Microsoft.EventHub/namespaces', variables('fullEventHubNameSpace'))]"
      ],
      "properties": {
        "APPINSIGHTS_INSTRUMENTATIONKEY": "[reference(resourceId('Microsoft.Insights/components', variables('appInsightsName'))).InstrumentationKey]",
        "KEYVAULT_PATH": "[parameters('keyVaultPath')]",
        "KEYVAULT_SECRET": "[parameters('keyVaultSecret')]",
        "OTHER_SITE": "[reference(concat('Microsoft.Web/sites/', variables('otherSiteName'))).hostnames[0]]",
        "DB_KEY": "[listKeys(resourceId(concat('Microsoft.DocumentDb/databaseAccounts'),variables('databaseAccountName')),'2015-04-08').primaryMasterKey]",
      }
    },
    {
      "apiVersion": "2015-08-01",
      "name": "Staging",
      "type": "slots",
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[resourceId('Microsoft.Web/Sites', variables('siteName'))]"
      ],
      "properties": {
      },
      "resources": [
        {
          "name": "appsettings",
          "type": "config",
          "apiVersion": "2015-08-01",
          "dependsOn": [
            "[concat('Microsoft.Web/sites/', variables('siteName'))]",
            "[concat('Microsoft.Insights/components/', variables('appInsightsName'))]",
            "[concat('Microsoft.DocumentDb/databaseAccounts/',variables('databaseAccountName'))]",
            "[concat('Microsoft.Web/sites/', variables('otherSiteName'))]",
            "[resourceid('Microsoft.EventHub/namespaces', variables('fullEventHubNameSpace'))]",
            "Staging"
          ],
          "properties": {
             "APPINSIGHTS_INSTRUMENTATIONKEY": "[reference(resourceId('Microsoft.Insights/components', variables('appInsightsName'))).InstrumentationKey]",
             "KEYVAULT_PATH": "[parameters('keyVaultPath')]",
             "KEYVAULT_SECRET": "[parameters('keyVaultSecret')]",
             "OTHER_SITE": "[reference(concat('Microsoft.Web/sites/', variables('otherSiteName'))).hostnames[0]]",
             "DB_KEY": "[listKeys(resourceId(concat('Microsoft.DocumentDb/databaseAccounts'),variables('databaseAccountName')),'2015-04-08').primaryMasterKey]",
          }
        }
      ]
    }
  ]
},

有没有办法在这个查询中对date2进行修改,所以它实际上是date2-1day?

类似的东西:

$query->whereRaw('date1 > date2')->get()

1 个答案:

答案 0 :(得分:2)

您可以在查询的“原始”部分调用任何SQL代码,因此您可以执行以下操作:

$query->whereRaw('date1 > DATE_SUB(date2, INTERVAL 1 DAY)')->get();

请记住,以这种方式执行SQL代码将使您的查询仅在支持此类函数的数据库中起作用。