全球过滤 - 如何在Laravel雄辩中使用全球范围 [英] Global filtering - how to use global scope in Laravel Eloquent
问题描述
?published = 0/1
): public function scopePublishedFilter($ query)
{
if(!Auth :: check())$ query-> where('published','= ',1);
else
{
$ published = Input :: get('published');
if(isset($ published))$ query-> where('published','=',$ published);
}
return $ query;
}
我应用于我的 ArticlesController
:
public function index()
{
return View :: make ('category')
- >('category')
- > with('tags')
- > publishedFilter()
- > get()
]);
}
关于文章关系:
public function articles()
{
return $ this-> hasMany('Article') - > publishedFilter();
}
但理想情况下,我只想在 如何确保所有的 更好的方法是粘贴一点太长,像 如果你想要,请阅读这个 http://softonsofa.com/laravel-how-to-define-and-use-eloquent-global-范围/ 1创建一个类 2通过调用 如果我是你,我会使用 编辑 I have a published filter that I use for my articles. Guests can only view published articles, logged in users can view and apply filter ( I apply this in my And on the article relationships: But ideally I would like to only define it in the How can I make sure that all returned articles from the Better way is a bit too long to paste it and works like Read this if you want it http://softonsofa.com/laravel-how-to-define-and-use-eloquent-global-scopes/ Short way: you need global scope for this. And here's how you do it in 2 steps (squashed a bit): 1 Create a class 2 Boot that class in your Eloquent model by calling If I were you I would use edit when you use this one with 这篇关于全球过滤 - 如何在Laravel雄辩中使用全球范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
SoftDeleting
内核中的东西。
已发布的
ScopeInterface
class PublishedScope实现ScopeInterface {
public function apply(Builder $ builder)
{
$ table = $ builder-> getModel() - > getTable();
$ builder-> where($ table。'。published','=',1);
$ this-> addWithDrafts($ builder);
}
public function remove(Builder $ builder)
{
$ query = $ builder-> getQuery();
$ column = $ builder-> getModel() - > getTable()。
$ bindingKey = 0;
foreach((array)$ query-> wheres as $ key => $ where)
{
if($ this-> isPublishedConstraint($ where,$ column))
{
unset($ query-> wheres [$ key]);
$ query-> wheres = array_values($ query-> wheres);
$ this-> removeBinding($ query,$ bindingKey);
}
//检查是否为NULL或NOT NULL类型,
//如果是这样,不要递增键
//,因为没有这些类型的绑定
if(!in_array($ where ['type'],['Null','NotNull']))$ bindingKey ++;
}
}
protected function removeBinding(Builder $ query,$ key)
{
$ bindings = $ query-> getRawBindings() '哪里'];
unset($ bindings [$ key]);
$ query-> setBindings($ bindings);
}
protected function addWithDrafts(Builder $ builder)
{
$ builder->宏('withDrafts',函数(Builder $ builder)
{
$ this-> remove($ builder);
return $ builder;
});
}
在您的雄辩模型中引导该类static :: addGlobalScope(new AbcScope)
//模型
public static function boot()
{
parent :: boot();
static :: addGlobalScope(new PublishedScope);
}
published_at
列,并检查 null
而不是 = 1
,但这取决于你。 p>
删除
方法更新 - 谢谢当使用此范围与 SoftDeletingTrait
时,将@Leon指向意外行为。当您使用 SoftDeletingScope
或另一个使用$($)的 NULL
或 NOT NULL
约束和此范围不是第一个使用的(是的, 使用
语句在这里),删除
方法将无法正常工作。它不会删除任何绑定,而不是它应该。?published=0/1
):public function scopePublishedFilter($query)
{
if(!Auth::check()) $query->where('published', '=', 1);
else
{
$published = Input::get('published');
if (isset($published)) $query->where('published', '=', $published);
}
return $query;
}
ArticlesController
:public function index()
{
return View::make('articles.index', [
'articles' => Article::with('owner')
->with('category')
->with('tags')
->publishedFilter()
->get()
]);
}
public function articles()
{
return $this->hasMany('Article')->publishedFilter();
}
Article
model itself, since it's easy to forget to include this filter when implementing new features or views.Article
model are run through this filter before returned?UPDATE: Just use this: https://github.com/jarektkaczyk/laravel-global-scope for global scopes in L5+
SoftDeleting
thing in the core.
PublishedScope
that implements ScopeInterface
class PublishedScope implements ScopeInterface {
public function apply(Builder $builder)
{
$table = $builder->getModel()->getTable();
$builder->where($table.'.published', '=', 1);
$this->addWithDrafts($builder);
}
public function remove(Builder $builder)
{
$query = $builder->getQuery();
$column = $builder->getModel()->getTable().'.published';
$bindingKey = 0;
foreach ((array) $query->wheres as $key => $where)
{
if ($this->isPublishedConstraint($where, $column))
{
unset($query->wheres[$key]);
$query->wheres = array_values($query->wheres);
$this->removeBinding($query, $bindingKey);
}
// Check if where is either NULL or NOT NULL type,
// if that's the case, don't increment the key
// since there is no binding for these types
if ( ! in_array($where['type'], ['Null', 'NotNull'])) $bindingKey++;
}
}
protected function removeBinding(Builder $query, $key)
{
$bindings = $query->getRawBindings()['where'];
unset($bindings[$key]);
$query->setBindings($bindings);
}
protected function addWithDrafts(Builder $builder)
{
$builder->macro('withDrafts', function(Builder $builder)
{
$this->remove($builder);
return $builder;
});
}
static::addGlobalScope(new AbcScope)
// the model
public static function boot()
{
parent::boot();
static::addGlobalScope(new PublishedScope);
}
published_at
column and check it for null
instead of = 1
, but that's up to you.
remove
method updated - thanks to @Leon for pointing out unexpected behaviour, when using this scope together with SoftDeletingTrait
. The problem is a bit deeper:SoftDeletingScope
or another one, that utilizes NULL
or NOT NULL
constraint and this scope is not the first one used (yes, order of use
statements matters here), remove
method will not work as expected. It will not remove any binding or not the one, that it should.