[英]Get view, comment, like counts of an article using Laravel5.5 Eloquent Query Builder
[英]Convert SQL to Eloquent Query Builder : Laravel5.5
如何將該SQL轉換為Laravel5.5雄辯的格式
select * from 'arm_articles' where ('article_tag' like '%standard%' or 'article_topic' like '%standard%' or 'article_details' like '%standard%' or 'article_type' like '%standard%') and ( ('id' between 287 and 296) and 'article_active' = 1) order by 'id' desc
請觀察SQL中的花括號
這是我寫的在使用-> toSql測試輸出時返回不同的sql的代碼
$post= PostModel::where('article_tag','like','%'.$contributor_id.'%')->orWhere('article_topic','like','%'.$contributor_id.'%')->orWhere('article_details','like','%'.$contributor_id.'%')->orWhere('article_type','like','%'.$contributor_id.'%')->whereBetween('id', [$end, $start-1])->where('article_active',1)->orderBy('id', 'desc')->take(10)->get();
從上面的查詢中找到SQL輸出
select * from 'arm_articles' where 'article_tag' like ? or 'article_topic' like ? or 'article_details' like ? or 'article_type' like ? and 'id' between ? and ? and 'article_active' = ? order by 'id' desc limit 10
此輸出看起來像所需的SQL,但是不同之處在於SQL上的花括號。 因此,雄辯的查詢生成器一詞隨查詢一起出現了嗎?
PostModel::where(function($q) use($contributor_id) {
$q->where('article_tag', 'like', '%' . $contributor_id . '%')
->orWhere('article_topic', 'like', '%' . $contributor_id . '%')
->orWhere('article_details', 'like', '%' . $contributor_id . '%')
->orWhere('article_type', 'like', '%' . $contributor_id . '%');
})
->whereBetween('id', [$end, $start - 1])
->where('article_active', 1)
->orderBy('id', 'desc')
->take(10)
->get();
試試這個
$post = PostModel::where(function ($query) use ($contributor_id) {
$query->orWhere('article_topic','like','%'.$contributor_id.'%')
->orWhere('article_details','like','%'.$contributor_id.'%')
->orWhere('article_type','like','%'.$contributor_id.'%')
->orwhere('article_tag','like','%'.$contributor_id.'%');
})->whereBetween('id', [$end, $start-1])
->where('article_active',1)
->orderBy('id', 'desc')
->take(10)
->get();
Alexey Mezenin在我之前做了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.