In laravel, scope is used to process data in the model. You can define methods at the beginning of the scope in the model. These methods can be called directly through the model and are called query scopes. The syntax is "public function scope" Capitalize the first letter of the word ($query){...}".
#The operating environment of this article: Windows 10 system, Laravel version 8, Dell G3 computer.
How to use scope in laravel
Scope scope in laravel
In laravel, the data (belonging or not) processed in the template (add, delete, modify, check) is introduced Use scope to handle
That is, add the prefix scope in the template definition method.
In short, the model in Laravel can define methods at the beginning of the scope, and these methods can be called directly through the model. This type of approach is also called query scope.
Conditional queries are often used in data queries. Some of these query conditions are the same. Scope can realize the reuse of these same query conditions.
Normal scope
Definition
The method name is scope with the first letter in capital letters (this can be defined at will)
public function scopeStatus($query) { return $query->where('status',1); }
Use
The function whose first letter is lowercase is called after removing the scope.
scopeStatus () calls status ().
User::status()->get();
Global scope
We need to redefine the boot method, integrate the parent class boot, and add the global scope, so that it will be used globally by default.
protected static function boot() { parent::boot(); // TODO: Change the autogenerated stub static::addGlobalScope('avaiable',function (Builder $builder){ $builder->whereIn('status', [0, 1]); }); }
Sometimes when we have queries that don’t need this global scope, we just need to remove it.
Use withOutGlobalScope() method
Post::withOutGlobalScope('avaiable')->get();
[Related recommendations: laravel video tutorial]
The above is the detailed content of How to use scope in laravel. For more information, please follow other related articles on the PHP Chinese website!

Collaborative document editing is an effective tool for distributed teams to optimize their workflows. It improves communication and project progress through real-time collaboration and feedback loops, and common tools include Google Docs, Microsoft Teams, and Notion. Pay attention to challenges such as version control and learning curve when using it.

ThepreviousversionofLaravelissupportedwithbugfixesforsixmonthsandsecurityfixesforoneyearafteranewmajorversion'srelease.Understandingthissupporttimelineiscrucialforplanningupgrades,ensuringprojectstability,andleveragingnewfeaturesandsecurityenhancemen

Laravelcanbeeffectivelyusedforbothfrontendandbackenddevelopment.1)Backend:UtilizeLaravel'sEloquentORMforsimplifieddatabaseinteractions.2)Frontend:LeverageBladetemplatesforcleanHTMLandintegrateVue.jsfordynamicSPAs,ensuringseamlessfrontend-backendinteg

Laravelcanbeusedforfullstackdevelopment.1)BackendmasterywithLaravel'sexpressivesyntaxandfeatureslikeEloquentORMfordatabasemanagement.2)FrontendintegrationusingBladefordynamicHTMLtemplates.3)EnhancingfrontendwithLaravelMixforassetcompilation.4)Fullsta

Answer: The best tools for upgrading Laravel include Laravel's UpgradeGuide, LaravelShift, Rector, Composer, and LaravelPint. 1. Use Laravel's UpgradeGuide as the upgrade roadmap. 2. Use LaravelShift to automate most of the upgrade work, but it requires manual review. 3. Automatically refactor the code through Rector, and you need to understand and possibly customize its rules. 4. Use Composer to manage dependencies and pay attention to possible dependency conflicts. 5. Run LaravelPint to maintain code style consistency, but it does not solve the functional problems.

ToenhanceengagementandcohesionamongdistributedteamsbeyondZoom,implementthesestrategies:1)Organizevirtualcoffeebreaksforinformalchats,2)UseasynchronoustoolslikeSlackfornon-workdiscussions,3)Introducegamificationwithteamgamesorchallenges,and4)Encourage

Laravel10introducesseveralbreakingchanges:1)ItrequiresPHP8.1orhigher,2)TheRouteServiceProvidernowusesabootmethodforloadingroutes,3)ThewithTimestamps()methodonEloquentrelationshipsisdeprecated,and4)TheRequestclassnowpreferstherules()methodforvalidatio

Tomaintainfocusandmotivationinremotework,createastructuredenvironment,managedigitaldistractions,fostermotivationthroughsocialinteractionsandgoalsetting,maintainwork-lifebalance,anduseappropriatetechnology.1)Setupadedicatedworkspaceandsticktoaroutine.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Chinese version
Chinese version, very easy to use
