关于php:Laravel-Eloquent-ORM-的本地作用域

48次阅读

共计 471 个字符,预计需要花费 2 分钟才能阅读完成。

定义:

class User extends Model
{public function scopePopular($query)
    {return $query->where('votes', '>', 100);
    }

    public function scopeActive($query)
    {return $query->where('active', 1);
    }
    
    public function scopeOfType($query, $type)
    {return $query->where('type', $type);
    }
}

应用:

$users = App\User::popular()->active()->ofType('admin')->orderBy('created_at')->get();

$users = App\User::popular()->orWhere(function (Builder $query) {$query->active();
})->get();
# 或者
$users = App\User::popular()->orWhere->active()->get();

正文完
 0