- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个用于文章的已发布过滤器。 guest 只能查看已发布的文章,登录用户可以查看和应用过滤器(?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
模型本身中定义它,因为在实现新功能或 View 时很容易忘记包含此过滤器。
如何确保所有从 Article
模型返回的文章在返回之前都经过此过滤器?
最佳答案
更好的方法是粘贴它有点太长,并且像核心中的 SoftDeleting
一样工作。
如果需要,请阅读此书 http://softonsofa.com/laravel-how-to-define-and-use-eloquent-global-scopes/
捷径:为此您需要全局范围。以下是分两步完成的方法(略微压缩):
1 创建一个实现ScopeInterface
的类PublishedScope
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;
});
}
2 通过调用 static::addGlobalScope(new AbcScope)
// the model
public static function boot()
{
parent::boot();
static::addGlobalScope(new PublishedScope);
}
如果我是你,我会使用 published_at
列并检查它是否为 null
而不是 = 1
,但这取决于你。
edit remove
方法已更新 - 感谢@Leon 指出在将此作用域与 SoftDeletingTrait
一起使用时的意外行为。问题有点深:
当你将这个与 SoftDeletingScope
或另一个一起使用时,它利用了 NULL
或 NOT NULL
约束 and此作用域不是第一个使用的(是的,use
语句的顺序在这里很重要),remove
方法将无法按预期工作。它不会删除任何绑定(bind),也不会删除它应该删除的绑定(bind)。
关于laravel - 全局过滤——如何在 Laravel Eloquent 中使用全局作用域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26095541/
我是 Laravel 的新手,我正在处理我的数据库迁移。对于一个表,我在表定义中包含了 $table->timestamps() 快捷方式。令我沮丧的是,我发现在我为表设置种子后,created_at
我有一个任务需要显示每种类型的假期,其中 id 是奇数。当我尝试在 Eloquent 中执行它时,它总是给我一个错误。 查询 Select holiday_type.id, holiday_type.
我有一个任务需要显示每种类型的假期,其中 id 是奇数。当我尝试在 Eloquent 中执行它时,它总是给我一个错误。 查询 Select holiday_type.id, holiday_type.
好的,我有型号 A链接到许多模型 B , 所以型号 A有一个 hasMany与模型的关系 B和型号B有一个 belongsTo与模型的关系 A . 但是,在所有这些中B有一个特定的,例如具有较高值的
如何应用 Laravel 的 Eloquent whereIn() 以便它包含 null? 我已经尝试过: User::whereIn("column", [null, 1, 2]) -> get()
我有一张库存变动表,其中显示了产品的历史记录(接收、移动位置等) 我有两个计算(通过总和)的查询: 原收到数量。 当前的实时数量(在事物被移动之后)。 这两个查询都返回相同模型的集合。它们将始终具有相
我一直在研究 Slim 2,最近才开始部署到生产服务器。到目前为止,一切似乎都运行正常。我能够登录,所以我知道我正在连接到数据库就好了。它识别出我登录的人,识别出我作为该用户拥有的权限。我有另一个表有
我在 Eloquent 中使用 orWhere 时遇到问题。 我有一个团队,这个团队有一些资料。我想获取状态 = 1 或状态 = 2 的所有配置文件。但我无法让它工作。 我的代码是这样的: $prof
在 Zizaco/entrust的 Laravel Entrust,如何设置与 Eloquent 的 Entrust 类关系,以便获得角色拥有的权限列表,如下所示: // get admin role
我想为我的两个表创建一个汇总报告,一个是员工表,另一个是 sims 表。这些表具有一对多的关系。我知道我们可以通过使用导出模型的数据->fromModel($model)但是有没有办法让我可以根据这两
我有两个模型,一个是扩展 Jenssegers\Model 的 mongo 模型,另一个是扩展 Illuminate\Model 的 sql 模型。这个 sql 模型没有定义连接名称,因为我们有多个数
我有两个多对一关系的模型: class Meal extends \Eloquent { /** * public Integer $id; - primary key
到目前为止,Vue2 对我来说读起来非常好,但除了这一点我很难调试。 Shop-show.vue ..... ..... {{shop.user.id}} .....
关闭。这个问题需要更多 focused .它目前不接受答案。 想要改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 2 年前。 Improve this q
我需要获取第二个表的 ID。我无法避免使用 INNER JOIN,因为我必须按第二个表中的列对结果进行排序。 这是我的两张 table events id name .... date
我正在尝试为我的用户提供 Laravel hiscores 分页表的排名数字。 这是我发现有效的 MySQL 查询。我正在尝试将其作为 Laravel Eloquent 查询来使用。 select @
我的产品模型上有这种关系。 public function productInventory(){ return $this->hasOne('App\Models\Ecommerce
我正在尝试在 Laravel 5.5 应用程序中为一个包含关系数据的表添加种子。 我有这两个表/模型: 用户 问题 在 app\User.php 模型文件上;我有以下 hasMany 关系: publ
我重写了这个帮助请求,并简化了我试图尽量减少移动部件数量的内容。 我是一名经验丰富的 OOP 程序员,但这是我使用 LAMP、Laravel 或 Eloquent 的第一个项目。 我正在 Larave
我正在使用 Laravel 和 MongoDB (jenssegers/laravel-mongodb) 开发一个网络应用程序。 在使用 php artisan make:model 创建新模型时,命
我是一名优秀的程序员,十分优秀!