2016-12-27 167 views
1

在SQL這是相當容易做到像下面這樣:Laravel:數學在WHERE子句

SELECT * FROM properties WHERE (price/acres) = 3000; 

這將選擇每個屬性裏的「價格每畝」是3000

我希望做這在Laravel。我試過如下:

Property::where('price/acres', 3000)->get(); 

但是這個包裹在反引號的列標題,創建下列SQL:與錯誤

select * from `properties` where `price/acres` = 3000 

這失敗(顯然):SQLSTATE[42S22]: Column not found: 1054 Unknown column 'price/acres' in 'where clause'

回答

6

您可以使用whereRaw是這樣的:

Property::whereRaw('(price/acres) = 3000')->get(); 
+0

沒關係啊,這是現在可能是一個更好的辦法,我想想吧! – DrewT

+0

沒有我想要的那麼優雅,因爲我現在不得不清理並逃避用戶輸入,但它起作用 – stevendesu

+2

如果您需要傳遞一個數字作爲變量,請將3000更改爲?,然後傳遞它。 'property :: whereRaw('(price/acres)=?',[$ yourvariable]) - > get();'這不能用列名完成。 – aynber