[英]Laravel aggregate eloquent max result
這是我想做的事情:
我在我的供應商表中實現了version_id。 我正在嘗試構造查詢以使索引頁正常工作。 因此,我希望從我的數據庫中獲得所有具有最新version_id(max('version_id')的供應商。似乎無法弄清楚。這是到目前為止我所擁有的:
$vendors = $this->account->vendors()->where('version_id', max(version_id))
->orderBy($this->orderBy, $this->sortBy)
->paginate($this->display_per_page);
我以前嘗試過此方法,但它也給我錯誤:
$vendors = $this->account->vendors()->max('version_id')
->orderBy($this->orderBy, $this->sortBy)
->paginate($this->display_per_page);
如果我只有這個,它就可以工作:
$vendors = $this->account->vendors()
->orderBy($this->orderBy, $this->sortBy)
->paginate($this->display_per_page);
一直在網上尋找,但我只找到有關聯接等查詢的建議,這不是我想要的。 任何幫助表示贊賞!
這應該工作:
$vendors = $this->account->vendors()
->whereRaw('version_id = (SELECT MAX(version_id) from vendors)')
->orderBy($this->orderBy, $this->sortBy)
->paginate($this->display_per_page);
假設表名vendors
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.