1"use strict";(self.webpackChunkfirestore_eloquent=self.webpackChunkfirestore_eloquent||[]).push([[5404],{2985:(e,o,n)=>{n.r(o),n.d(o,{assets:()=>l,contentTitle:()=>i,default:()=>u,frontMatter:()=>t,metadata:()=>a,toc:()=>c});var r=n(5893),s=n(1151);const t={sidebar_position:11},i="Query Scopes",a={id:"guide/eloquent/query-scopes",title:"Query Scopes",description:"Global scopes allow you to add constraints to all queries for a given model.",source:"@site/docs/guide/eloquent/query-scopes.mdx",sourceDirName:"guide/eloquent",slug:"/guide/eloquent/query-scopes",permalink:"/docs/guide/eloquent/query-scopes",draft:!1,unlisted:!1,editUrl:"https://github.com/FreddyWhest/firestore-eloquent-docs/tree/main/docs/guide/eloquent/query-scopes.mdx",tags:[],version:"current",sidebarPosition:11,frontMatter:{sidebar_position:11},sidebar:"tutorialSidebar",previous:{title:"Like Method",permalink:"/docs/guide/eloquent/like-method"},next:{title:"Middlewares",permalink:"/docs/guide/middleware"}},l={},c=[{value:"Local Scopes",id:"local-scopes",level:2},{value:"Utilizing a Local Scope",id:"utilizing-a-local-scope",level:3},{value:"Dynamic Scopes",id:"dynamic-scopes",level:3}];function d(e){const o={code:"code",h1:"h1",h2:"h2",h3:"h3",p:"p",pre:"pre",...(0,s.a)(),...e.components};return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)(o.h1,{id:"query-scopes",children:"Query Scopes"}),"\n",(0,r.jsx)(o.p,{children:"Global scopes allow you to add constraints to all queries for a given model."}),"\n",(0,r.jsx)(o.h2,{id:"local-scopes",children:"Local Scopes"}),"\n",(0,r.jsx)(o.p,{children:'Local scopes allow you to define common sets of query constraints that you may easily re-use throughout your application. For example, you may need to frequently retrieve all users that are considered "popular". To define a scope, prefix an Eloquent model method with scope.'}),"\n",(0,r.jsx)(o.pre,{children:(0,r.jsx)(o.code,{className:"language-php",children:"//Post Model\r\nuse Roddy\\FirestoreEloquent\\Facade\\FModel;\r\nuse Roddy\\FirestoreEloquent\\Firestore\\Eloquent\\Traits\\FRelations;\r\n\r\nclass Post extends FModel\r\n{\r\n //Import this class\r\n use FRelations;\r\n\r\n protected $collection = 'posts';\r\n protected $primaryKey = 'post_id';\r\n protected $fillable = ['title', 'content', 'user_id'];\r\n\r\n //Local Scope\r\n public function scopePopular($query)\r\n {\r\n return $query->where(['likes', '>', 100]);\r\n }\r\n}\n"})}),"\n",(0,r.jsx)(o.h3,{id:"utilizing-a-local-scope",children:"Utilizing a Local Scope"}),"\n",(0,r.jsx)(o.p,{children:"Once the scope has been defined, you may call the scope methods when querying the model. However, you should not include the scope prefix when calling the method. You can even chain calls to various scopes:"}),"\n",(0,r.jsx)(o.pre,{children:(0,r.jsx)(o.code,{className:"language-php",children:"use App\\FModel\\Post;\r\n\r\n//Find popular posts\r\n$popularPosts = Post::popular()->get();\n"})}),"\n",(0,r.jsx)(o.h3,{id:"dynamic-scopes",children:"Dynamic Scopes"}),"\n",(0,r.jsx)(o.p,{children:"Sometimes you may wish to define a scope that accepts parameters. To get started, just add your additional parameters to your scope method's signature. Scope parameters should be defined after the $query parameter:"}),"\n",(0,r.jsx)(o.pre,{children:(0,r.jsx)(o.code,{className:"language-php",children:"//User Model\r\nnamespace App\\FModels;\r\n \r\nuse Roddy\\FirestoreEloquent\\Facade\\FModel;\r\nuse Roddy\\FirestoreEloquent\\Firestore\\Eloquent\\Traits\\FRelations;\r\n \r\nclass User extends FModel\r\n{\r\n /**\r\n * Scope a query to only include users of a given type.\r\n */\r\n public function scopeOfType($query, string $type)\r\n {\r\n return $query->where(['type', $type]);\r\n }\r\n}\n"})}),"\n",(0,r.jsx)(o.p,{children:"Once the expected arguments have been added to your scope method's signature, you may pass the arguments when calling the scope:"}),"\n",(0,r.jsx)(o.pre,{children:(0,r.jsx)(o.code,{className:"language-php",children:"//Find users of a given type \r\n$users = User::ofType('admin')->get();\r\n\r\n//OR\r\n\r\n$users = User::ofType('admin')->orWhere(['age', '>', 30])->get();\n"})})]})}function u(e={}){const{wrapper:o}={...(0,s.a)(),...e.components};return o?(0,r.jsx)(o,{...e,children:(0,r.jsx)(d,{...e})}):d(e)}},1151:(e,o,n)=>{n.d(o,{Z:()=>a,a:()=>i});var r=n(7294);const s={},t=r.createContext(s);function i(e){const o=r.useContext(t);return r.useMemo((function(){return"function"==typeof e?e(o):{...o,...e}}),[o,e])}function a(e){let o;return o=e.disableParentContext?"function"==typeof e.components?e.components(s):e.components||s:i(e.components),r.createElement(t.Provider,{value:o},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.