1"use strict";(self.webpackChunkfirestore_eloquent=self.webpackChunkfirestore_eloquent||[]).push([[3361],{8272:(e,n,o)=>{o.r(n),o.d(n,{assets:()=>u,contentTitle:()=>r,default:()=>a,frontMatter:()=>s,metadata:()=>c,toc:()=>l});var t=o(5893),i=o(1151);const s={sidebar_position:8},r="Counting Documents",c={id:"guide/eloquent/counting-data",title:"Counting Documents",description:"Counting Documents in Firestore Eloquent Library",source:"@site/versioned_docs/version-2.x/guide/eloquent/counting-data.mdx",sourceDirName:"guide/eloquent",slug:"/guide/eloquent/counting-data",permalink:"/docs/2.x/guide/eloquent/counting-data",draft:!1,unlisted:!1,editUrl:"https://github.com/FreddyWhest/firestore-eloquent-docs/tree/main/versioned_docs/version-2.x/guide/eloquent/counting-data.mdx",tags:[],version:"2.x",sidebarPosition:8,frontMatter:{sidebar_position:8},sidebar:"tutorialSidebar",previous:{title:"Pagination",permalink:"/docs/2.x/guide/eloquent/pagination"},next:{title:"Sub collection",permalink:"/docs/2.x/guide/eloquent/sub-collection"}},u={},l=[{value:"Counting Documents in Firestore Eloquent Library",id:"counting-documents-in-firestore-eloquent-library",level:2},{value:"Counting All Documents",id:"counting-all-documents",level:3},{value:"Counting Documents with Conditions",id:"counting-documents-with-conditions",level:3},{value:"Counting Documents with Multiple Conditions",id:"counting-documents-with-multiple-conditions",level:3},{value:"Conclusion",id:"conclusion",level:2}];function d(e){const n={code:"code",h1:"h1",h2:"h2",h3:"h3",p:"p",pre:"pre",strong:"strong",...(0,i.a)(),...e.components};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsx)(n.h1,{id:"counting-documents",children:"Counting Documents"}),"\n",(0,t.jsx)(n.h2,{id:"counting-documents-in-firestore-eloquent-library",children:"Counting Documents in Firestore Eloquent Library"}),"\n",(0,t.jsx)(n.p,{children:"Counting documents is a common operation to retrieve the number of records that match certain conditions. The Firestore Eloquent Library in Laravel provides a straightforward way to count documents based on specified criteria. Let's explore how to count documents using the library's features."}),"\n",(0,t.jsx)(n.h3,{id:"counting-all-documents",children:"Counting All Documents"}),"\n",(0,t.jsxs)(n.p,{children:["To count all documents in a Firestore collection, you can use the ",(0,t.jsx)(n.strong,{children:(0,t.jsx)(n.code,{children:"count()"})})," method:"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-php",children:"// Count all users\r\n$totalUsers = User::count();\n"})}),"\n",(0,t.jsx)(n.h3,{id:"counting-documents-with-conditions",children:"Counting Documents with Conditions"}),"\n",(0,t.jsxs)(n.p,{children:['You can also count documents based on certain conditions. For example, to count all users with the name "John Doe", you can use the ',(0,t.jsx)(n.strong,{children:(0,t.jsx)(n.code,{children:"where()"})})," method:"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-php",children:"// Count all users with the name \"John Doe\"\r\n$totalUsers = User::where('name', 'John Doe')->count();\n"})}),"\n",(0,t.jsx)(n.h3,{id:"counting-documents-with-multiple-conditions",children:"Counting Documents with Multiple Conditions"}),"\n",(0,t.jsxs)(n.p,{children:['You can also count documents based on multiple conditions. For example, to count all users with the name "John Doe" and the age of 30, you can use the ',(0,t.jsx)(n.strong,{children:(0,t.jsx)(n.code,{children:"andWhere()"})})," method or the ",(0,t.jsx)(n.strong,{children:(0,t.jsx)(n.code,{children:"orWhere()"})})," method:"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-php",children:"// Count all users with the name \"John Doe\" and the age of 30\r\n$totalUsers = User::where(['name', '=', 'John Doe'], ['age', '>', '30'])->count();\r\n\r\n// Count all users with the name \"John Doe\" or the age of 30\r\n$totalUsers = User::orWhere(['age', '>', '30'], ['name', '=', 'John Doe'])->count();\n"})}),"\n",(0,t.jsx)(n.h2,{id:"conclusion",children:"Conclusion"}),"\n",(0,t.jsx)(n.p,{children:"Counting documents in Firestore with the Firestore Eloquent Library is a simple yet powerful operation. Whether you need to count all documents, apply conditions for c
1ounting, or work with soft-deleted records, the library provides convenient methods for these scenarios. Choose the method that best fits your application's requirements and use it to efficiently retrieve document counts based on specified criteria."})]})}function a(e={}){const{wrapper:n}={...(0,i.a)(),...e.components};return n?(0,t.jsx)(n,{...e,children:(0,t.jsx)(d,{...e})}):d(e)}},1151:(e,n,o)=>{o.d(n,{Z:()=>c,a:()=>r});var t=o(7294);const i={},s=t.createContext(i);function r(e){const n=t.useContext(s);return t.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function c(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(i):e.components||i:r(e.components),t.createElement(s.Provider,{value:n},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.