搜索
首页php框架Laravellaravel中的scope怎么用

在laravel中,scope用于处理模型中的数据,在模型中可以定义scope开头方法,这类方法可以通过模型直接调用,被称为查询作用域,语法为“public function scope首字母大写单词($query){...}”。

laravel中的scope怎么用

本文操作环境:Windows10系统、Laravel8版、Dell G3电脑。

laravel中的scope怎么用

laravel中的scope作用域

laravel中在模板中处理(属于不属于)的数据(增删改查),引入了scope来处理

也就是在模板定义方法中,加上前缀scope。

简言之,Laravel中模型中可以定义scope开头方法,这类方法可以通过模型直接调用。这类方法也称作查询作用域。

在数据查询中经常会使用条件查询,这些查询条件有些是相同的,scope可以实现这些相同条件查询的复用。

普通 scope

定义

方法名为 scope + 首字母大写单词 (这个可以随便定义)

public function scopeStatus($query)
{
    return $query->where('status',1);
}

使用

其中调用去掉 scope 后,首字母小写的那个函数。

scopeStatus () 调用 status ()。

User::status()->get();

全局 scope

我们需要重新定义 boot 方法,集成父类 boot 以后,添加全局 scope,这样默认就已经全局使用了。

protected static function boot()
{
    parent::boot(); // TODO: Change the autogenerated stub
    static::addGlobalScope('avaiable',function (Builder $builder){
        $builder->whereIn('status', [0, 1]);
    });
}

我们有的时候有的查询是不需要这个全局 scope 的时候,我们只需要去掉就可以。

使用 withOutGlobalScope()方法

Post::withOutGlobalScope('avaiable')->get();

【相关推荐:laravel视频教程

以上是laravel中的scope怎么用的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
如何使用Laravel迁移:逐步教程如何使用Laravel迁移:逐步教程May 13, 2025 am 12:15 AM

laravelmigrationsStreamLinedAtabasemangementbyallowingbolAlyChemachangeStobEdeDinedInphpcode,whobeversion-controllolleDandShared.here'showtousethem:1)createMigrationClassestodeFinePerationFineFineOperationsLikeCreatingingModifyingTables.2)

查找最新的Laravel版本:快速简便的指南查找最新的Laravel版本:快速简便的指南May 13, 2025 am 12:13 AM

要查找最新版本的Laravel,可以访问官方网站laravel.com并点击右上角的"Docs"按钮,或使用Composer命令"composershowlaravel/framework|grepversions"。保持更新有助于提升项目安全性和性能,但需考虑对现有项目的影响。

使用Laravel的更新:使用最新版本的好处使用Laravel的更新:使用最新版本的好处May 13, 2025 am 12:08 AM

youshouldupdateTotheLateStlaravelVerverSionForPerformanceImprovements,增强的安全性,newfeatures,BetterCommunitySupport,and long-term-Maintenance.1)绩效:Laravel9'Selover9'seloquentormoptimizatizationenenhanceApplicationsPeed.2)secuse:laravel8InIntrododeDodecter.2)

Laravel:我搞砸了我的迁移,我该怎么办?Laravel:我搞砸了我的迁移,我该怎么办?May 13, 2025 am 12:06 AM

当您的inoumessupamigrationInlaravel,youcan:1)滚动播放'phpartisanmigrate:rollback'ifit'Sthelastone,or'phpartisanmigrate:reset'forall; 2)crecteAneAnewmigrateTocorrectRateRectRectRateRectRectRectRectRectRectRectRectErcrationInproduction; 3)

最后的Laravel版本:性能指南最后的Laravel版本:性能指南May 13, 2025 am 12:04 AM

toboostPerformanceInthelateStlaravelversion,关注详细信息:1)用户disforcachingtoimproveresponsetimes和Reddiccistatabaseload.2)优化的AtabasequesquesquesquesquesquesquesquesqueriesWitheAgerloadingTopreventn 1Queryissues.3)emplortecachingInprodododododododoductuproutroutrououtrououToreSourte。

最新的Laravel版本:DIFES DISCON最新的Laravel版本:DIFES DISCONMay 12, 2025 am 12:15 AM

Laravel10IntroducessEveralKeyFeatUrestHatenHanceWebDevelopment.1)LazyCollectionsAllyCollefficeProcesingOflargeFlargedAtasetSwithSwithOutloadingAllRecordSintomeMemory.2)the Make:Model Model Moged-and-Mogration'ArtisanCommandSancancMommandSimplififieScreatingModeltigation.3)

Laravel迁移解释了:创建,修改和管理您的数据库Laravel迁移解释了:创建,修改和管理您的数据库May 12, 2025 am 12:11 AM

laravelmigrationssshouldbeusedbecausetheystreamlinedeplupment,nesurecresistencyAcrossenviments和simplifyCollaborationAndDeployment.1)shemallogragrammatonofdatabaseschemachanges,ReeducingErrors.2)MigrigationScanBeverCanbeverSionConconconconcontrollin.2)

Laravel迁移:值得使用吗?Laravel迁移:值得使用吗?May 12, 2025 am 12:10 AM

是的。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具