laravel query Scope
Brief introduction
Presented here is a local query scope,
Local inquiries Scope :
This condition is commonly used where the package model,
adjusted to a method where every time.
Global Query Scope :
Each call is the model query conditions will be filtered,
feeling little scenarios, we did not see, not introduced.
use
- In the model, create a scope prefix plus methods to
class News extends Model
{
const TABLE = 'news';
//只包含展示的新闻的查询作用域
public function scopeScoShow($query)
{
return $query->where('is_show', 1);
}
//只包含某个类型的查询作用域,带参数
public function scopeScoType($query, $type)
{
return $query->where('type', $type);
}
}
transfer
//使用时调用,首字母小写
News::scoShow()->get();
//带参数
News::scoShow()->scoType(2)->get();