Php Larvel检索模型时的全局范围
我正在从数据库中检索事件,每次从数据库中获取事件时,我都想检查active\ u from的位置,您需要创建一个类似EventsTrait的特征,并在该特征内添加一个BootEventStrait函数,该函数将添加一个类似EventsScope的全局范围。使用SoftDeletingTrait作为模式 事件特征Php Larvel检索模型时的全局范围,php,laravel,laravel-4,eloquent,Php,Laravel,Laravel 4,Eloquent,我正在从数据库中检索事件,每次从数据库中获取事件时,我都想检查active\ u from的位置,您需要创建一个类似EventsTrait的特征,并在该特征内添加一个BootEventStrait函数,该函数将添加一个类似EventsScope的全局范围。使用SoftDeletingTrait作为模式 事件特征 trait EventsTrait { public static function bootEventsTrait() { static::addGl
trait EventsTrait {
public static function bootEventsTrait()
{
static::addGlobalScope(new EventsScope);
}
}
事件范围
use Illuminate\Database\Eloquent\ScopeInterface;
use Illuminate\Database\Eloquent\Builder;
class EventScope implements ScopeInterface {
public function apply(Builder $builder)
{
$builder->where("active_from","<=", "today");
}
public function remove(Builder $builder)
{
// remove scope
}
}
您可以向事件模型添加全局作用域,例如:is_active
// App\Event.php
use Carbon\Carbon;
public static function boot(){
parent::boot();
static::addGlobalScope('is_active', function($builder){
$builder->where('active_from', '<=', Carbon::now());
})
}
查看@Anam我已经看过了,但是我找不到在我尝试获取模型时如何指定执行它。下面是如何完成的