在laravel中,scope用于处理模型中的数据,在模型中可以定义scope开头方法,这类方法可以通过模型直接调用,被称为查询作用域,语法为“public function scope首字母大写单词($query){...}”。
本文操作环境: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中文网其他相关文章!

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

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

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

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

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

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

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


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

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

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

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

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具