1"use strict";(self.webpackChunkfirestore_eloquent=self.webpackChunkfirestore_eloquent||[]).push([[5062],{9389:(e,n,i)=>{i.r(n),i.d(n,{assets:()=>d,contentTitle:()=>r,default:()=>u,frontMatter:()=>o,metadata:()=>t,toc:()=>c});var s=i(5893),l=i(1151);const o={sidebar_position:9},r="Sub collection",t={id:"guide/eloquent/sub-collection",title:"Sub collection",description:"A sub-collection is a nested collection within a Firestore document. This allows for better data organization and structuring.",source:"@site/docs/guide/eloquent/sub-collection.mdx",sourceDirName:"guide/eloquent",slug:"/guide/eloquent/sub-collection",permalink:"/docs/guide/eloquent/sub-collection",draft:!1,unlisted:!1,editUrl:"https://github.com/FreddyWhest/firestore-eloquent-docs/tree/main/docs/guide/eloquent/sub-collection.mdx",tags:[],version:"current",sidebarPosition:9,frontMatter:{sidebar_position:9},sidebar:"tutorialSidebar",previous:{title:"Aggregations",permalink:"/docs/guide/eloquent/aggragations"},next:{title:"Like Method",permalink:"/docs/guide/eloquent/like-method"}},d={},c=[{value:"Defining a sub collection",id:"defining-a-sub-collection",level:2},{value:"Understanding the <code>SubCollection</code> Class Explained",id:"understanding-the-subcollection-class-explained",level:2},{value:"Main Methods:",id:"main-methods",level:3},{value:"Example Usage of <code>SubCollection</code>:",id:"example-usage-of-subcollection",level:3},{value:"Usage of the sub collection",id:"usage-of-the-sub-collection",level:3},{value:"Summary",id:"summary",level:2}];function a(e){const n={admonition:"admonition",code:"code",h1:"h1",h2:"h2",h3:"h3",li:"li",p:"p",pre:"pre",strong:"strong",ul:"ul",...(0,l.a)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(n.h1,{id:"sub-collection",children:"Sub collection"}),"\n",(0,s.jsx)(n.p,{children:"A sub-collection is a nested collection within a Firestore document. This allows for better data organization and structuring."}),"\n",(0,s.jsx)(n.h2,{id:"defining-a-sub-collection",children:"Defining a sub collection"}),"\n",(0,s.jsx)(n.p,{children:"In your model the model class, define a method that starts with sub, followed by the sub-collection name."}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-php",children:"class User extends FModel\r\n{\r\n public function subProfile()\r\n {\r\n return SubCollection::primaryKey(\"id\")->subCollection('profiles')->load();\r\n }\r\n}\n"})}),"\n",(0,s.jsx)(n.p,{children:"In this example:"}),"\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.code,{children:"subProfile()"})," defines a sub-collection called ",(0,s.jsx)(n.code,{children:"profiles"}),"."]}),"\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.code,{children:'primaryKey("id")'})," specifies that the primary key for this sub-collection is ",(0,s.jsx)(n.code,{children:"id"}),"."]}),"\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.code,{children:"load()"})," fetches the sub-collection data."]}),"\n"]}),"\n",(0,s.jsx)(n.admonition,{type:"info",children:(0,s.jsxs)(n.p,{children:["Note: Make sure you start the method name with ",(0,s.jsx)(n.code,{children:"sub"})," followed by the name of the sub collection."]})}),"\n",(0,s.jsxs)(n.h2,{id:"understanding-the-subcollection-class-explained",children:["Understanding the ",(0,s.jsx)(n.code,{children:"SubCollection"})," Class Explained"]}),"\n",(0,s.jsxs)(n.p,{children:["The ",(0,s.jsx)(n.code,{children:"SubCollection"})," class provides methods to configure and retrieve sub-collections."]}),"\n",(0,s.jsx)(n.h3,{id:"main-methods",children:"Main Methods:"}),"\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.strong,{children:(0,s.jsx)(n.code,{children:"subCollection($name)"})})," \u2192 Sets the sub-collection name. ",(0,s.jsx)(n.strong,{children:"[required]"})]}),"\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.strong,{children:(0,s.jsx)(n.code,{children:"primaryKey($key)"})})," \u2192 Defines the primary key of the sub-collection. ",(0,s.jsx)(n.strong,{children:"[required]"})]}),"\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.strong,{children:(0,s.jsx)(n.code,{children:"fillableFields($fields)"})})," \u2192 Specifies which fields can be inserted or updated."]}),"\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.strong,{children:(0,s.jsx)(n.code,{children:"requiredFields($fields)"})})," \u2192 Lists fields that must be provided when inserting data."]}),"\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.strong,{children:(0,s.jsx)(n.code,{children:"defaultFields($fields)"})})," \u2192 Assigns default values to fields if they are not provided."]}),"\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.strong,{children:(0,s.jsx)(n.code,{children:"fieldTypes($fields)"})})," \u2192 Defines data types (",(0,s.jsx)(n.code,{children:"integer, string, array, object"}),")."]}),"\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.strong,{children:(0,s.jsx)(n.code,{children:"hiddenFields($fields)"})})," \u2192 Specifies fields to exclude from responses."]}),"\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.strong,{children:(0,s.jsx)(n.code,{children:"keyToUse($key)"})})," \u2192 By default, the primary key of the parent model (",(0,s.jsx)(n.code,{children:"User"}),") is used to access sub-collections. If you want to use a different field, you can set it using this method."]}),"\n",(0,s.jsxs)(n.li,{children:[(0,s.jsx)(n.strong,{children:(0,s.jsx)(n.code,{children:"load()"})})," \u2192 Fetches the sub-collection data."]}),"\n"]}),"\n",(0,s.jsxs)(n.h3,{id:"example-usage-of-subcollection",children:["Example Usage of ",(0,s.jsx)(n.code,{children:"SubCollection"}),":"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-php",children:"SubCollection::primaryKey(\"id\")\r\n ->subCollection('user_logs')\r\n ->keyToUse(\"user_ID\")\r\n ->fillableFields(['log_id', 'action', 'timestamp'])\r\n ->load();\n"})}),"\n",(0,s.jsx)(n.p,{children:"This means:"}),"\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsxs)(n.li,{children:["A sub-collection named ",(0,s.jsx)(n.code,{children:"user_logs"})," is being defined."]}),"\n",(0,s.jsxs)(n.li,{children:["The primary key is ",(0,s.jsx)(n.code,{children:"id"}),"."]}),"\n",(0,s.jsxs)(n.li,{children:["Instead of using the default primary key from ",(0,s.jsx)(n.code,{children:"ZoneData"}),", it will use ",(0,s.jsx)(n.code,{children:"user_ID"})," to access sub-collections."]}),"\n",(0,s.jsxs)(n.li,{children:["Only ",(0,s.jsx)(n.code,{children:"log_id"}),", ",(0,s.jsx)(n.code,{children:"action"}),", and ",(0,s.jsx)(n.code,{children:"timestamp"})," can be inserted or updated."]}),"\n"]}),"\n",(0,s.jsx)(n.h3,{id:"usage-of-the-sub-collection",children:"Usage of the sub collection"}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-php",children:"class User extends FModel\r\n{\r\n public function subProfile()\r\n {\r\n return SubCollection::primaryKey(\"id\")->subCollection('profiles')->load();\r\n }\r\n}\r\n\r\n\r\n$user = User::find($userId); \r\n//Or \r\n$user = User::where(['id', $userId])->first();\r\n\r\n//Getting the first item\r\n$profile = $user->subProfile()->first();\r\n\r\n//Filtering the data\r\n$profile = $user->subProfile()->where(['id', '123'])->first();\r\n\r\n//Getting all items\r\n$profiles = $user->subProfile()->all();\r\n\r\n//Getting the count of items\r\n$count = $user->subProfile()->count();\r\n\n"})}),"\n",(0,s.jsx)(n.h2,{id:"summary",children:"Summary"}),"\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsx)(n.li,{children:"A sub-collection is a collection inside a document."}),"\n",(0,s.jsxs)(n.li,{children:["To define one, create a method in your model starting with ",(0,s.jsx)(n.code,{children:"sub"}),"."]}),"\n",(0,s.jsxs)(n.li,{children:["Use the ",(0,s.jsx)(n.code,{children:"SubCollection"})," class to configure and retrieve sub-collections."]}),"\n",(0,s.jsx)(n.li,{children:"Define fields, primary keys, required values, and more using provided methods."}),"\n",(0,s.jsxs)(n.li,{children:["Use ",(0,s.jsx)(n.code,{children:"keyToUse($key)"})," if you want to reference sub-collections using a different field instead of the default primary key."]}),"\n"]}),"\n",(0,s.jsx)(n.p,{children:"This approach ensures better Firestore organization and easy data retrieval. \ud83d\ude80"})]})}function u(e={}){const{wrapper:n}={...(0,l.a)(),...e.components};return n?(0,s.jsx)(n,{...e,children:(0,s.jsx)(a,{...e})}):a(e)}},1151:(e,n,i)=>{i.d(n,{Z:()=>t,a:()=>r});var s=i(7294);const l={},o=s.createContext(l);function r(e){const n=s.useContext(o);return s.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function t(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(l):e.components||l:r(e.components),s.createElement(o.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.