-2
如何在Laravel 5.1雄辯生成器中查詢給定的sql?Laravel雄辯查詢生成器
"SELECT referer, count(referer) AS listRefererCount FROM sss_options WHERE shorturl=:shorturl GROUP BY referer"
如何在Laravel 5.1雄辯生成器中查詢給定的sql?Laravel雄辯查詢生成器
"SELECT referer, count(referer) AS listRefererCount FROM sss_options WHERE shorturl=:shorturl GROUP BY referer"
這是給你的。
YourModel::select('referer',DB::raw('count(referer) AS listRefererCount')
->where('shorturl','=',$variable)
->groupBy('referer')
->get();
謝謝你,就是這樣。 –
// "SELECT referer, count(referer) AS listRefererCount FROM sss_options WHERE shorturl=:shorturl GROUP BY referer"
// this is the model class
class SssOptions extends \Illuminate\Database\Eloquent\Model
{
protected $table = 'sss_options';
}
// below is the query builder
$q = SssOptions::query();
$shorturl = 'your cond';
$q->getQuery()
->select(['referer', new \Illuminate\Database\Query\Expression('count(referer) AS listRefererCount')])
->where('shorturl', $shorturl)
->groupBy('referer')
;
var_dump($q->toSql()); // the sql
var_dump($q->get()); // the result
你嘗試過什麼或搜索呢?查詢其實很簡單。 – James
你應該先試試自己,如果你失敗了,那就請求幫助。但至少表現出一些努力,這是一個簡單的查詢構建。例如閱讀laravel文檔。 –
如果我可以,我不會問Stackoverflow。如果你真的可以幫忙,請給我我的答案。 謝謝 –