【问题标题】:Eloquent Make a long queryEloquent 进行长查询
【发布时间】:2018-09-24 15:25:48
【问题描述】:
如何使用多个 where 语句和 orderby 2 种类型进行 Eloquent 查询并对其进行分页或限制?
PostIco::table('posts')
->where('listingType', '!=', 1)
->OrderBy('listingType', 'created_at')
->limit(25)
->paginate(10)
如何让它工作?
【问题讨论】:
标签:
mysql
laravel
class
phpmyadmin
eloquent
【解决方案1】:
PostIco 是一个雄辩的模型吗?如果是这样,你就不要在上面使用table 方法。
PostIco::where('listingType', '!=', 1)
// Instead of OrderBy
->orderBy('listingType', 'asc')
->orderBy('created_at', 'desc')
->limit(25)
->paginate(10);
你也可以使用DB facade:
DB::table('posts')
->where('listingType', '!=', 1)
->orderBy('listingType', 'asc')
->orderBy('created_at', 'desc')
->limit(25)
->paginate(10);
编辑:更正 orderBy 语句
【解决方案2】:
对于多个 where 子句,您可以这样做:
PostIco::where('listingType', '!=', 1)->where('status', 1) // and you can add chain of wheres
->orderBy('listingType')
->orderBy('created_at', 'desc')
->limit(25)
->paginate(10);
// OR
PostIco::where('listingType', '!=', 1)->orWhere('status', 1) // and you can add chain of wheres and orWheres
->orderBy('listingType', 'asc')
->limit(25)
->paginate(10);