Model Laravel4雄辩的复合词在重载的newQuery()模型方法中的何处?
我需要为模型创建一个默认的全局范围 Laravel文档使用匿名函数对以下条件进行分组:Model Laravel4雄辩的复合词在重载的newQuery()模型方法中的何处?,model,laravel-4,scope,eloquent,Model,Laravel 4,Scope,Eloquent,我需要为模型创建一个默认的全局范围 Laravel文档使用匿名函数对以下条件进行分组: DB::table('users') ->where('name', '=', 'John') ->orWhere(function($query) { $query->where('votes', '>', 100) ->where('title', '<>'
DB::table('users')
->where('name', '=', 'John')
->orWhere(function($query)
{
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
->get();
我得到一个502错误的网关!在该方法中,我能找出如何进行复合的唯一方法是执行原始查询:
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
public function newQuery()
{
return parent::newQuery()
->whereRaw('(foreign_id = ? or role = ?)')
->setBindings( array( MyClass::$id, 'admin' ) );
}
有人知道为什么会发生这样的事吗,但没有明确的答案。这需要全球范围 下面是一个基本示例(有关框架附带的浏览器
SoftDeletingScope
和SoftDeletingTrait
的更多信息):
//模型
类PopularUser扩展了用户{
受保护的$table='users';
公共静态函数boot()
{
父::boot();
静态::addGlobalScope(新的PopularUserScope);
}
}
//范围
使用Illumb\Database\Elount\Builder;
使用Illumb\Database\Elount\ScopeInterface;
类PopularUserScope实现ScopeInterface{
公共功能应用(生成器$Builder)
{
$builder->where('投票','>',100)
->其中(‘标题’、‘管理’);
}
公共函数remove(Builder$Builder){//在您的案例中不需要它}
}
您需要这样做的是旧版本的框架吗?全局作用域用于此,而不是newQuery
@JarekTkaczyk\u deczo\u。感谢您提供的信息!我想当时我正在寻找如何正确地完成它,我不知道默认情况下过滤关注的术语“全局范围”。我来看看这个,让我们把它作为答案
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
public function newQuery()
{
return parent::newQuery()
->whereRaw('(foreign_id = ? or role = ?)')
->setBindings( array( MyClass::$id, 'admin' ) );
}
// Model
class PopularUser extends User {
protected $table = 'users';
public static function boot()
{
parent::boot();
static::addGlobalScope(new PopularUserScope);
}
}
// Scope
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\ScopeInterface;
class PopularUserScope implements ScopeInterface {
public function apply(Builder $builder)
{
$builder->where('votes', '>', 100)
->where('title', '<>', 'Admin');
}
public function remove(Builder $builder) { // you don't need this in your case }
}