kay*_*yex 5 scope accessor laravel eloquent
我有一个模型,我需要检查值并返回一个不健康的状态.我创建了一个Accessor,它正在工作并按预期返回true或false.
$task->unhealthy()
Run Code Online (Sandbox Code Playgroud)
访问者代码
public function getUnhealthyAttribute(){
//Is in Active status
if ( $this->status_id == 1 ){
return true;
}
//Has overdue items
if ( $this->items()->overdue()->count() > 0 ) {
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
我现在需要检索所有"不健康"任务的集合.
问题:是否可以将我的Accessor与范围一起使用?什么是正确的方法?
一旦您拥有包含所有任务的集合,您就可以使用集合的filter()
方法仅过滤不健康的任务:
$unhealthy_tasks = $tasks->filter(function($task, $key) {
return $task->unhealthy; // if returns true, will be in $unhealthy_tasks
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
361 次 |
最近记录: |