1"use strict";(self.webpackChunkfirestore_eloquent=self.webpackChunkfirestore_eloquent||[]).push([[8679],{2865:(e,r,n)=>{n.r(r),n.d(r,{assets:()=>a,contentTitle:()=>d,default:()=>h,frontMatter:()=>t,metadata:()=>o,toc:()=>l});var i=n(5893),s=n(1151);n(7294);const t={sidebar_position:2},d="Retrieving data",o={id:"guide/eloquent/retrieving-data",title:"Retrieving data",description:"The library provides flexible and convenient methods for retrieving documents from Firestore. Whether you want to retrieve all documents, filter based on specific criteria, or find a document by its ID.",source:"@site/versioned_docs/version-2.x/guide/eloquent/retrieving-data.mdx",sourceDirName:"guide/eloquent",slug:"/guide/eloquent/retrieving-data",permalink:"/docs/2.x/guide/eloquent/retrieving-data",draft:!1,unlisted:!1,editUrl:"https://github.com/FreddyWhest/firestore-eloquent-docs/tree/main/versioned_docs/version-2.x/guide/eloquent/retrieving-data.mdx",tags:[],version:"2.x",sidebarPosition:2,frontMatter:{sidebar_position:2},sidebar:"tutorialSidebar",previous:{title:"Model Configuration",permalink:"/docs/2.x/guide/eloquent/model-configuration"},next:{title:"Updating data",permalink:"/docs/2.x/guide/eloquent/updating-data"}},a={},l=[{value:"Retrieving first documents",id:"retrieving-first-documents",level:2},{value:"<code>first()</code> method",id:"first-method",level:3},{value:"<code>firstOrFail()</code> method",id:"firstorfail-method",level:3},{value:"<code>find(string $documentId)</code> method",id:"findstring-documentid-method",level:3},{value:"Retrieving all documents",id:"retrieving-all-documents",level:2},{value:"<code>all()</code> method",id:"all-method",level:3},{value:"Example Use Case",id:"example-use-case",level:4},{value:"Paginating Results",id:"paginating-results",level:3},{value:"Filtering Documents",id:"filtering-documents",level:2},{value:"Basic Filtering with <code>where()</code>",id:"basic-filtering-with-where",level:3},{value:"Advanced Filtering with <code>andWhere()</code> and <code>orWhere()</code>",id:"advanced-filtering-with-andwhere-and-orwhere",level:3},{value:"AND conditions",id:"and-conditions",level:4},{value:"OR conditions",id:"or-conditions",level:4},{value:"Ordering Results with <code>orderBy()</code>, <code>orderByDesc()</code>, and <code>orderByAsc()</code>",id:"ordering-results-with-orderby-orderbydesc-and-orderbyasc",level:3},{value:"Chaining Methods",id:"chaining-methods",level:3},{value:"Determining If Records Exist",id:"determining-if-records-exist",level:2},{value:"Limiting Results",id:"limiting-results",level:2}];function c(e){const r={a:"a",admonition:"admonition",code:"code",h1:"h1",h2:"h2",h3:"h3",h4:"h4",p:"p",pre:"pre",strong:"strong",...(0,s.a)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(r.h1,{id:"retrieving-data",children:"Retrieving data"}),"\n",(0,i.jsx)(r.p,{children:"The library provides flexible and convenient methods for retrieving documents from Firestore. Whether you want to retrieve all documents, filter based on specific criteria, or find a document by its ID."}),"\n",(0,i.jsx)(r.admonition,{title:"Note",type:"info",children:(0,i.jsxs)(r.p,{children:["With version >= 2.x of the library, to get the data of the document, you need to call the ",(0,i.jsx)(r.code,{children:"data()"})," method on the retrieved document. This method returns the data of the document as an object.",(0,i.jsx)("br",{}),"\r\n",(0,i.jsx)(r.strong,{children:"See below for more details and examples"})]})}),"\n",(0,i.jsx)(r.h2,{id:"retrieving-first-documents",children:"Retrieving first documents"}),"\n",(0,i.jsxs)(r.h3,{id:"first-method",children:[(0,i.jsx)(r.code,{children:"first()"})," method"]}),"\n",(0,i.jsxs)(r.p,{children:["The ",(0,i.jsx)(r.strong,{children:(0,i.jsx)(r.code,{children:"first()"})})," method is used to retrieve the first document from the Firestore database that matches the current query parameters. If there are multiple documents that meet the criteria, only the first one encountered will be returned."]}),"\n",(0,i.jsx)(r.pre,{children:(0,i.jsx)(r.code,{className:"language-php",children:"// Retrieves the first user with age 20\r\n$user = User::where(['age' => 20])->first();\r\n\r\n// Retrieves the first user without any filters\r\n$user = User::first();\r\n\r\n// Get the retrieved user data\r\n$userData = $user->data();\r\n\r\n// Get the user ID\r\n$userId = $userData->id;\r\n\r\n// Get the user name\r\n$userName = $userData->name;\n"})}),"\n",(0,i.jsxs)(r.h3,{id:"firstorfail-method",children:[(0,i.jsx)(r.code,{children:"firstOrFail()"})," method"]}),"\n",(0,i.jsxs)(r.p,{children:["The ",(0,i.jsx)(r.strong,{children:(0,i.jsx)(r.code,{children:"firstOrFail()"})})," method is used to retrieve the first document from the Firestore database that matches the current query parameters. If there are multiple documents that meet the criteria, only the first one encountered will be returned. If no document is found, an exception will be thrown."]}),"\n",(0,i.jsx)(r.pre,{children:(0,i.jsx)(r.code,{className:"language-php",children:"// Retrieves the first user with age 20\r\n$user = User::where(['age' => 20])->firstOrFail();\r\n\r\n// Retrieves the first user without any filters\r\n$user = User::firstOrFail();\r\n\r\n// Get the retrieved user data\r\n$userData = $user->data();\r\n\r\n// Get the user ID\r\n$userId = $userData->id;\r\n\r\n// Get the user name\r\n$userName = $userData->name;\n"})}),"\n",(0,i.jsxs)(r.h3,{id:"findstring-documentid-method",children:[(0,i.jsx)(r.code,{children:"find(string $documentId)"})," method"]}),"\n",(0,i.jsxs)(r.p,{children:["The ",(0,i.jsx)(r.strong,{children:(0,i.jsx)(r.code,{children:"find()"})})," method is used to retrieve a document from the Firestore database by its ID."]}),"\n",(0,i.jsx)(r.pre,{children:(0,i.jsx)(r.code,{className:"language-php",children:"// Retrieves the user with ID 123\r\n$user = User::find('123');\r\n\r\n// Get the retrieved user data\r\n$userData = $user->data();\r\n\r\n// Get the user ID\r\n$userId = $userData->id;\r\n\r\n// Get the user name\r\n$userName = $userData->name;\n"})}
1),"\n",(0,i.jsx)(r.h2,{id:"retrieving-all-documents",children:"Retrieving all documents"}),"\n",(0,i.jsxs)(r.p,{children:["Retrieving all documents from a Firestore collection is a common use case, and the Firestore Eloquent Library in Laravel simplifies this process with the ",(0,i.jsx)(r.code,{children:"all()"})," method. This section will guide you through using this method and provide additional insights."]}),"\n",(0,i.jsxs)(r.h3,{id:"all-method",children:[(0,i.jsx)(r.code,{children:"all()"})," method"]}),"\n",(0,i.jsxs)(r.p,{children:["The ",(0,i.jsx)(r.strong,{children:(0,i.jsx)(r.code,{children:"all()"})})," method is used to retrieve all documents from a Firestore collection. This method is useful when you want to retrieve all documents from a collection without any filtering or sorting."]}),"\n",(0,i.jsx)(r.pre,{children:(0,i.jsx)(r.code,{className:"language-php",children:"// Retrieves all users\r\n$users = User::all();\n"})}),"\n",(0,i.jsx)(r.h4,{id:"example-use-case",children:"Example Use Case"}),"\n",(0,i.jsxs)(r.p,{children:["The ",(0,i.jsx)(r.strong,{children:(0,i.jsx)(r.code,{children:"all()"})})," method is useful when you want to retrieve all documents from a collection without any filtering or sorting."]}),"\n",(0,i.jsx)(r.pre,{children:(0,i.jsx)(r.code,{className:"language-php",children:"// UserController.php\r\n\r\npublic function index()\r\n{\r\n $users = User::all();\r\n\r\n return view('users.index', compact('users'));\r\n}\n"})}),"\n",(0,i.jsx)(r.pre,{children:(0,i.jsx)(r.code,{className:"language-php",children:"// index.blade.php\r\n\r\n@foreach ($users->data() as $user)\r\n <p>This is user {{ $user->name }}</p>\r\n@endforeach\n"})}),"\n",(0,i.jsx)(r.h3,{id:"paginating-results",children:"Paginating Results"}),"\n",(0,i.jsx)(r.p,{children:"If your Firestore collection contains a large number of documents, consider paginating the results to improve performance and user experience. Laravel provides a simple pagination feature that you can use in combination with the Firestore Eloquent Library."}),"\n",(0,i.jsx)(r.pre,{children:(0,i.jsx)(r.code,{className:"language-php",children:"// UserController.php\r\n\r\npublic function index()\r\n{\r\n $users = User::paginate(10);\r\n\r\n return view('users.index', compact('users'));\r\n}\n"})}),"\n",(0,i.jsx)(r.pre,{children:(0,i.jsx)(r.code,{className:"language-php",children:"// index.blade.php\r\n\r\n@foreach ($users->data() as $user)\r\n <p>This is user {{ $user->name }}</p>\r\n@endforeach\r\n\r\n{!! $users->links() !!}\n"})}),"\n",(0,i.jsxs)(r.p,{children:["The ",(0,i.jsx)(r.strong,{children:(0,i.jsx)(r.code,{children:"links()"})})," method generates the necessary pagination links for navigating through the result set."]}),"\n",(0,i.jsx)(r.admonition,{title:"MORE ABOUT PAGINATION",type:"info",children:(0,i.jsxs)(r.p,{children:["For more information about pagination, please refer to the ",(0,i.jsx)(r.a,{href:"/docs/guide/eloquent/pagination",children:"Laravel documentation"}),"."]})}),"\n",(0,i.jsx)(r.h2,{id:"filtering-documents",children:"Filtering Documents"}),"\n",(0,i.jsx)(r.p,{children:"Filtering documents is a crucial aspect when working with Firestore, and the Firestore Eloquent Library in Laravel provides various methods to construct precise queries. Let's explore advanced techniques for filtering documents."}),"\n",(0,i.jsx)(r.admonition,{type:"warning",children:(0,i.jsxs)(r.p,{children:["Make sure you add the filter fields to the Firestore indexes to avoid errors. ",(0,i.jsx)(r.a,{href:"https://firebase.google.com/docs/firestore/query-data/indexing",children:"Read more about Firestore indexes"}),"."]})}),"\n",(0,i.jsxs)(r.h3,{id:"basic-filtering-with-where",children:["Basic Filtering with ",(0,i.jsx)(r.code,{children:"where()"})]}),"\n",(0,i.jsxs)(r.p,{children:["The ",(0,i.jsx)(r.strong,{children:(0,i.jsx)(r.code,{children:"where()"})})," method allows you to filter documents based on one or more criteria. It takes an associative array where keys represent field names, and values represent the conditions:"]}),"\n",(0,i.jsx)(r.admonition,{type:"warning",children:(0,i.jsxs)(r.p,{children:["Make sure you add the filter fields to the Firestore indexes to avoid errors. ",(0,i.jsx)(r.a,{href:"https://firebase.google.com/docs/firestore/query-data/indexing",children:"Read more about Firestore indexes"}),"."]})}),"\n",(0,i.jsx)(r.pre,{children:(0,i.jsx)(r.code,{className:"language-php",children:"// Retrieve users with age 25\r\n$filteredUsers = User::where(['age', '=', 25])->get();// index.blade.php\r\n\r\n// in your blade file\r\n@foreach ($filteredUsers->data() as $filteredUser)\r\n <p>This is user {{ $filteredUser->name }}</p>\r\n@endforeach\n"})}
1),"\n",(0,i.jsxs)(r.p,{children:["You can also use different comparison operators, such as ",(0,i.jsx)(r.code,{children:"'=', '==', '>', '<', '>=', and '<='"}),", to create more complex queries:"]}),"\n",(0,i.jsx)(r.pre,{children:(0,i.jsx)(r.code,{className:"language-php",children:"\r\n// Retrieve users with age greater than 25\r\n$filteredUsers = User::where(['age', '>', 25])->get();\r\n\r\n// Retrieve users with age less than or equal to 25\r\n$filteredUsers = User::where(['age', '<=', 25])->get();\r\n\r\n// in your blade file\r\n@foreach ($filteredUsers->data() as $filteredUser)\r\n <p>This is user {{ $filteredUser->name }}</p>\r\n@endforeach\n"})}),"\n",(0,i.jsx)(r.admonition,{type:"warning",children:(0,i.jsxs)(r.p,{children:["Make sure you add the filter fields to the Firestore indexes to avoid errors. ",(0,i.jsx)(r.a,{href:"https://firebase.google.com/docs/firestore/query-data/indexing",children:"Read more about Firestore indexes"}),"."]})}),"\n",(0,i.jsxs)(r.h3,{id:"advanced-filtering-with-andwhere-and-orwhere",children:["Advanced Filtering with ",(0,i.jsx)(r.code,{children:"andWhere()"})," and ",(0,i.jsx)(r.code,{children:"orWhere()"})]}),"\n",(0,i.jsxs)(r.p,{children:["For more complex queries involving AND and OR conditions, you can use ",(0,i.jsx)(r.code,{children:"andWhere()"})," and ",(0,i.jsx)(r.code,{children:"orWhere()"})," methods:"]}),"\n",(0,i.jsx)(r.admonition,{type:"warning",children:(0,i.jsxs)(r.p,{children:["Make sure you add the filter fields to the Firestore indexes to avoid errors. ",(0,i.jsx)(r.a,{href:"https://firebase.google.com/docs/firestore/query-data/indexing",children:"Read more about Firestore indexes"}),"."]})}),"\n",(0,i.jsx)(r.h4,{id:"and-conditions",children:"AND conditions"}),"\n",(0,i.jsx)(r.pre,{children:(0,i.jsx)(r.code,{className:"language-php",children:"// Retrieve users named 'John' and aged 25\r\n$andFilteredUsers = User::andWhere([['name', '=', 'John'], ['age' => 25]])->get();\r\n\r\n// in your blade file\r\n@foreach ($andFilteredUsers->data() as $andFilteredUser)\r\n <p>This is user {{ $andFilteredUser->name }}</p>\r\n@endforeach\r\n\n"})}),"\n",(0,i.jsx)(r.h4,{id:"or-conditions",children:"OR conditions"}),"\n",(0,i.jsx)(r.pre,{children:(0,i.jsx)(r.code,{className:"language-php",children:"// Retrieve users named 'John' or aged 25\r\n$orFilteredUsers = User::orWhere([['name', '=', 'John'], ['age' => 25]])->get();\r\n\n"})}),"\n",(0,i.jsx)(r.p,{children:"These methods provide a powerful way to construct intricate filters for your Firestore queries."}),"\n",(0,i.jsxs)(r.h3,{id:"ordering-results-with-orderby-orderbydesc-and-orderbyasc",children:["Ordering Results with ",(0,i.jsx)(r.code,{children:"orderBy()"}),", ",(0,i.jsx)(r.code,{children:"orderByDesc()"}),", and ",(0,i.jsx)(r.code,{children:"orderByAsc()"})]}),"\n",(0,i.jsx)(r.admonition,{type:"warning",children:(0,i.jsxs)(r.p,{children:["Make sure you add the filter fields to the Firestore indexes to avoid errors. ",(0,i.jsx)(r.a,{href:"https://firebase.google.com/docs/firestore/query-data/indexing",children:"Read more about Firestore indexes"}),"."]})}),"\n",(0,i.jsxs)(r.p,{children:["Sorting the results is often necessary, and you can achieve this using the ",(0,i.jsx)(r.code,{children:"orderBy()"}),", ",(0,i.jsx)(r.code,{children:"orderByDesc()"}),", and ",(0,i.jsx)(r.code,{children:"orderByAsc()"})," methods:"]}),"\n",(0,i.jsx)(r.pre,{children:(0,i.jsx)(r.code,{className:"language-php",children:"// Order users by ID in descending order\r\n$descOrderedUsers = User::orderByDesc('id')->get();\r\n\r\n// Order users by ID in ascending order\r\n$ascOrderedUsers = User::orderByAsc('id')->get();\r\n\r\n// Order users by ID in ascending order\r\n$orderedUsers = User::orderBy('id', 'ASC')->get();\r\n\r\n// Order users by ID in descending order\r\n$orderedUsers = User::orderBy('id', 'DESC')->get();\n"})}),"\n",(0,i.jsx)(r.p,{children:"You can also combine ordering with filtering for more fine-grained control over your queries."}),"\n",(0,i.jsx)(r.h3,{id:"chaining-methods",children:"Chaining Methods"}),"\n",(0,i.jsx)(r.p,{children:"One of the strengths of the library is the ability to chain methods, allowing you to build complex queries in a readable and expressive way:"}),"\n",(0,i.jsx)(r.pre,{children:(0,i.jsx)(r.code,{className:"language-php",children:"// Retrieve users with age 25, ordered by ID in descending order\r\n$filteredAndOrderedUsers = User::where(['age', '=', 25])->orderByDesc('id')->get();\r\n\n"})}),"\n",(0,i.jsx)(r.p,{children:"Chaining methods makes it easy to construct queries dynamically based on your application's needs."}),"\n",(0,i.jsx)(r.h2,{id:"determining-if-records-exist",children:"Determining If Records Exist"}
1),"\n",(0,i.jsxs)(r.p,{children:["Sometimes you may need to determine if any records exist in a collection. The Firestore Eloquent Library provides a convenient ",(0,i.jsx)(r.code,{children:"exists()"})," method that you can use to check if any records exist in a collection:"]}),"\n",(0,i.jsx)(r.pre,{children:(0,i.jsx)(r.code,{className:"language-php",children:"// Check if any users exist\r\n$documentId = '1234567890'; // The ID of the document to retrieve\r\n$user = User::find($documentId);\r\n$exists = $user->exists(); // return bool [true/false]\n"})}),"\n",(0,i.jsx)(r.pre,{children:(0,i.jsx)(r.code,{className:"language-php",children:"// Check if any users exist\r\n\r\n$user = User::where(['user_id', '=', 2])->first();\r\n$exists = $user->exists(); // return bool [true/false]\n"})}),"\n",(0,i.jsx)(r.h2,{id:"limiting-results",children:"Limiting Results"}),"\n",(0,i.jsxs)(r.p,{children:["The Firestore Eloquent Library provides a convenient ",(0,i.jsx)(r.code,{children:"limit()"})," method that you can use to limit the number of documents returned by a query:"]}),"\n",(0,i.jsx)(r.pre,{children:(0,i.jsx)(r.code,{className:"language-php",children:"// Retrieve the first 10 users\r\n$users = User::limit(10);\n"})}),"\n",(0,i.jsx)(r.pre,{children:(0,i.jsx)(r.code,{className:"language-php",children:"// Retrieve the first 10 users with age 25\r\n$users = User::where(['age', '=', 25])->limit(10);\n"})}),"\n",(0,i.jsx)(r.pre,{children:(0,i.jsx)(r.code,{className:"language-php",children:"// Retrieve the first 10 users with age 25, ordered by ID in descending order\r\n$users = User::where(['age', '=', 25])->orderByDesc('age')->limit(10);\n"})}),"\n",(0,i.jsx)(r.pre,{children:(0,i.jsx)(r.code,{className:"language-php",children:"// Retrieve the first 10 users with age 25, ordered by ID in descending order\r\n$users = User::where(['age', '=', 25])->orderByDesc('age')->limit(10);\n"})})]})}function h(e={}){const{wrapper:r}={...(0,s.a)(),...e.components};return r?(0,i.jsx)(r,{...e,children:(0,i.jsx)(c,{...e})}):c(e)}},1151:(e,r,n)=>{n.d(r,{Z:()=>o,a:()=>d});var i=n(7294);const s={},t=i.createContext(s);function d(e){const r=i.useContext(t);return i.useMemo((function(){return"function"==typeof e?e(r):{...r,...e}}),[r,e])}function o(e){let r;return r=e.disableParentContext?"function"==typeof e.components?e.components(s):e.components||s:d(e.components),i.createElement(t.Provider,{value:r},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.