1"use strict";(self.webpackChunkfirestore_eloquent=self.webpackChunkfirestore_eloquent||[]).push([[9658],{4724:(e,n,s)=>{s.r(n),s.d(n,{assets:()=>d,contentTitle:()=>t,default:()=>c,frontMatter:()=>o,metadata:()=>l,toc:()=>a});var r=s(5893),i=s(1151);const o={sidebar_position:6},t="Relationships",l={id:"guide/eloquent/relationships",title:"Relationships",description:"`hasOne` Relationship",source:"@site/docs/guide/eloquent/relationships.mdx",sourceDirName:"guide/eloquent",slug:"/guide/eloquent/relationships",permalink:"/docs/guide/eloquent/relationships",draft:!1,unlisted:!1,editUrl:"https://github.com/FreddyWhest/firestore-eloquent-docs/tree/main/docs/guide/eloquent/relationships.mdx",tags:[],version:"current",sidebarPosition:6,frontMatter:{sidebar_position:6},sidebar:"tutorialSidebar",previous:{title:"Deleting Documents",permalink:"/docs/guide/eloquent/deleting-data"},next:{title:"Pagination",permalink:"/docs/guide/eloquent/pagination"}},d={},a=[{value:"<code>hasOne</code> Relationship",id:"hasone-relationship",level:2},{value:"Accessing the Related Model",id:"accessing-the-related-model",level:3},{value:"Example 1: Finding a User and their Profile of a User",id:"example-1-finding-a-user-and-their-profile-of-a-user",level:3},{value:"Example 2: Finding a User and their Profile of Users",id:"example-2-finding-a-user-and-their-profile-of-users",level:3},{value:"<code>hasMany</code> Relationship",id:"hasmany-relationship",level:2},{value:"Defining a <code>hasMany</code> Relationship",id:"defining-a-hasmany-relationship",level:3},{value:"Accessing the Related Model",id:"accessing-the-related-model-1",level:3},{value:"Example 1: Finding a User and their Posts",id:"example-1-finding-a-user-and-their-posts",level:3},{value:"Example 2: Finding a User and their Posts of a User",id:"example-2-finding-a-user-and-their-posts-of-a-user",level:3},{value:"<code>belongsTo</code> Relationship",id:"belongsto-relationship",level:2},{value:"Defining a <code>belongsTo</code> Relationship",id:"defining-a-belongsto-relationship",level:3},{value:"Accessing the Related Model",id:"accessing-the-related-model-2",level:3},{value:"Example 1: Finding a Post and its Author",id:"example-1-finding-a-post-and-its-author",level:3}];function h(e){const n={code:"code",h1:"h1",h2:"h2",h3:"h3",li:"li",p:"p",pre:"pre",strong:"strong",ul:"ul",...(0,i.a)(),...e.components};return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)(n.h1,{id:"relationships",children:"Relationships"}),"\n",(0,r.jsxs)(n.h2,{id:"hasone-relationship",children:[(0,r.jsx)(n.code,{children:"hasOne"})," Relationship"]}),"\n",(0,r.jsxs)(n.p,{children:["The ",(0,r.jsx)(n.code,{children:"hasOne"})," relationship is used to define a one-to-one relationship between two models. For example, a ",(0,r.jsx)(n.code,{children:"User"})," model might have one ",(0,r.jsx)(n.code,{children:"Phone"})," model:"]}),"\n",(0,r.jsxs)(n.p,{children:["Defining a ",(0,r.jsx)(n.strong,{children:(0,r.jsx)(n.code,{children:"hasOne"})})," Relationship Let's explore the components of defining a hasOne relationship:"]}),"\n",(0,r.jsx)(n.p,{children:(0,r.jsx)(n.code,{children:"hasOne($model, $foreignKey, $localKey)"})}),"\n",(0,r.jsxs)(n.ul,{children:["\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsxs)(n.strong,{children:[(0,r.jsx)(n.code,{children:"$model"}),":"]})," The related model class name (e.g., ",(0,r.jsx)(n.strong,{children:"'Profile'"}),"). This is the class of the model that the current model (User) is related to."]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsxs)(n.strong,{children:[(0,r.jsx)(n.code,{children:"$foreignKey"}),":"]})," The foreign key in the related model's table that references the primary key of the current model (User). For example, if the Profile model has a column named ",(0,r.jsx)(n.code,{children:"user_id"})," that references the id column in the User model, you would specify ",(0,r.jsx)(n.code,{children:"'user_id'"})," as the ",(0,r.jsx)(n.code,{children:"$foreignKey"}),"."]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsxs)(n.strong,{children:[(0,r.jsx)(n.code,{children:"$localKey"}),":"]})," The local key of the current model (User). This is the primary key by default. If the primary key is named something other than
1id, you can specify it here. In the example, 'id' is used as the default."]}),"\n"]}),"\n"]}),"\n",(0,r.jsx)(n.h3,{id:"accessing-the-related-model",children:"Accessing the Related Model"}),"\n",(0,r.jsx)(n.p,{children:"Once the relationship is defined, you can access the related model using the hasOne relationship."}),"\n",(0,r.jsx)(n.h3,{id:"example-1-finding-a-user-and-their-profile-of-a-user",children:"Example 1: Finding a User and their Profile of a User"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-php",children:"// User Model\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 //Import this class\r\n use FRelations;\r\n\r\n protected $collection = 'users';\r\n protected $primaryKey = 'user_id';\r\n protected $fillable = ['name', 'email', 'password'];\r\n protected $hidden = ['password'];\r\n\r\n\r\n //HasOne Relationship\r\n public function profile()\r\n {\r\n return $this->fHasOne(Profile::class, 'user_id', 'id');\r\n }\r\n}\r\n\r\n\r\n//Find users by a specific condition and get a collection\r\n$user = User::where(['user_id', 'user1'])->first();\r\n\r\n//Access the user's profile\r\n$profile = $user->profile();\r\n\r\n//OR\r\n\r\n$profile = $user->profile;\r\n\n"})}),"\n",(0,r.jsx)(n.h3,{id:"example-2-finding-a-user-and-their-profile-of-users",children:"Example 2: Finding a User and their Profile of Users"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-php",children:"//User Model\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 //Import this class\r\n use FRelations;\r\n\r\n protected $collection = 'users';\r\n protected $primaryKey = 'user_id';\r\n protected $fillable = ['name', 'email', 'password'];\r\n protected $hidden = ['password'];\r\n\r\n\r\n //HasOne Relationship\r\n public function profile()\r\n {\r\n return $this->fHasOne(Profile::class, 'user_id', 'id');\r\n }\r\n}\r\n\r\n\r\n//Find users by a specific condition and get a collection\r\n$users = User::where(['user_id', '=', 'user1'])->get();\r\n\r\n//Access the user's profile\r\n$users->each(function ($user) {\r\n $profile = $user->profile();\r\n});\r\n\n"})}),"\n",(0,r.jsxs)(n.h2,{id:"hasmany-relationship",children:[(0,r.jsx)(n.code,{children:"hasMany"})," Relationship"]}),"\n",(0,r.jsxs)(n.p,{children:["In Eloquent, the ",(0,r.jsx)(n.strong,{children:(0,r.jsx)(n.code,{children:"hasMany"})})," relationship is used to define a one-to-many relationship between two models. This means that each instance of the current model (e.g., ",(0,r.jsx)(n.strong,{children:"User"}),") can have multiple related records in another model (e.g., ",(0,r.jsx)(n.strong,{children:"Post"}),"). The relationship is typically established through a foreign key in the related model that references the primary key of the current model."]}),"\n",(0,r.jsxs)(n.h3,{id:"defining-a-hasmany-relationship",children:["Defining a ",(0,r.jsx)(n.code,{children:"hasMany"})," Relationship"]}),"\n",(0,r.jsx)(n.p,{children:"Let's explore the components of defining a hasMany relationship:"}),"\n",(0,r.jsx)(n.p,{children:(0,r.jsx)(n.code,{children:"hasMany($model, $foreignKey, $localKey)"})}),"\n",(0,r.jsxs)(n.ul,{children:["\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsxs)(n.strong,{children:[(0,r.jsx)(n.code,{children:"$model"}),":"]})," The related model class name (e.g., ",(0,r.jsx)(n.strong,{children:"'Post'"}),"). This is the class of the model that the current model (User) is related to."]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsxs)(n.strong,{children:[(0,r.jsx)(n.code,{children:"$foreignKey"}),":"]})," The foreign key in the related model's table that references the primary key of the current model (User). For example, if the Post model has a column named ",(0,r.jsx)(n.code,{children:"user_id"})," that references the id column in the User model, you would specify ",(0,r.jsx)(n.code,{children:"'user_id'"})," as the ",(0,r.jsx)(n.code,{children:"$foreignKey"}),"."]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsxs)(n.strong,{children:[(0,r.jsx)(n.code,{children:"$localKey"}),":"]})," The local key of the current model (User). This is the primary key by default. If the primary key is named something other than
1id, you can specify it here. In the example, 'id' is used as the default."]}),"\n"]}),"\n"]}),"\n",(0,r.jsx)(n.h3,{id:"accessing-the-related-model-1",children:"Accessing the Related Model"}),"\n",(0,r.jsx)(n.p,{children:"Once the relationship is defined, you can access the related model using the hasMany relationship."}),"\n",(0,r.jsx)(n.h3,{id:"example-1-finding-a-user-and-their-posts",children:"Example 1: Finding a User and their Posts"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-php",children:"//User Model\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 //Import this class\r\n use FRelations;\r\n \r\n protected $collection = 'users';\r\n protected $primaryKey = 'user_id';\r\n protected $fillable = ['name', 'email', 'password'];\r\n protected $hidden = ['password'];\r\n\r\n //HasMany Relationship\r\n public function posts()\r\n {\r\n return $this->fHasMany(Post::class, 'user_id', 'id');\r\n }\r\n}\r\n\r\n\r\n//Find users by a specific condition and get a collection\r\n$users = User::where(['user_id', '=', 'user1'])->get();\r\n\r\n//Access the user's posts\r\n$users->each(function ($user) {\r\n $posts = $user->posts();\r\n});\r\n\n"})}),"\n",(0,r.jsx)(n.h3,{id:"example-2-finding-a-user-and-their-posts-of-a-user",children:"Example 2: Finding a User and their Posts of a User"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-php",children:"//User Model\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 //Import this class\r\n use FRelations;\r\n\r\n protected $collection = 'users';\r\n protected $primaryKey = 'user_id';\r\n protected $fillable = ['name', 'email', 'password'];\r\n protected $hidden = ['password'];\r\n\r\n //HasMany Relationship\r\n public function posts()\r\n {\r\n return $this->fHasMany(Post::class, 'user_id', 'id');\r\n }\r\n}\r\n\r\n\r\n//Find users by a specific condition and get a collection\r\n$user = User::where(['user_id', '=', 'user1'])->first();\r\n\r\n\r\n//Access the user's posts\r\n$posts = $user->posts();\r\n});\n"})}),"\n",(0,r.jsxs)(n.h2,{id:"belongsto-relationship",children:[(0,r.jsx)(n.code,{children:"belongsTo"})," Relationship"]}),"\n",(0,r.jsxs)(n.p,{children:["The ",(0,r.jsx)(n.code,{children:"belongsTo"})," relationship is used to define a one-to-one or one-to-many relationship between two models. This means that each instance of the related model (e.g., ",(0,r.jsx)(n.strong,{children:"Post"}),") can have one or more related records in the current model (e.g., ",(0,r.jsx)(n.strong,{children:"User"}),"). The relationship is typically established through a foreign key in the current model that references the primary key of the related model."]}),"\n",(0,r.jsxs)(n.h3,{id:"defining-a-belongsto-relationship",children:["Defining a ",(0,r.jsx)(n.code,{children:"belongsTo"})," Relationship"]}),"\n",(0,r.jsx)(n.p,{children:"Let's explore the components of defining a belongsTo relationship:"}),"\n",(0,r.jsx)(n.p,{children:(0,r.jsx)(n.code,{children:"belongsTo($model, $foreignKey, $localKey)"})}),"\n",(0,r.jsxs)(n.ul,{children:["\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsxs)(n.strong,{children:[(0,r.jsx)(n.code,{children:"$model"}),":"]})," The related model class name (e.g., ",(0,r.jsx)(n.strong,{children:"'Post'"}),"). This is the class of the model that the current model (User) is related to."]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsxs)(n.strong,{children:[(0,r.jsx)(n.code,{children:"$foreignKey"}),":"]})," The foreign key in the current model's table that references the primary key of the related model (User). For example, if the Post model has a column named ",(0,r.jsx)(n.code,{children:"user_id"})," that references the id column in the User model, you would specify ",(0,r.jsx)(n.code,{children:"'user_id'"})," as the ",(0,r.jsx)(n.code,{children:"$foreignKey"}),"."]}),"\n"]}),"\n",(0,r.jsxs)(n.li,{children:["\n",(0,r.jsxs)(n.p,{children:[(0,r.jsxs)(n.strong,{children:[(0,r.jsx)(n.code,{children:"$localKey"}),":"]})," The local key of the current model (User). This is the primary key by default. If the primary key is named something other than
1id, you can specify it here. In the example, 'id' is used as the default."]}),"\n"]}),"\n"]}),"\n",(0,r.jsx)(n.h3,{id:"accessing-the-related-model-2",children:"Accessing the Related Model"}),"\n",(0,r.jsx)(n.p,{children:"Once the relationship is defined, you can access the related model using the belongsTo relationship."}),"\n",(0,r.jsx)(n.h3,{id:"example-1-finding-a-post-and-its-author",children:"Example 1: Finding a Post and its Author"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.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 //BelongsTo Relationship\r\n public function user()\r\n {\r\n return $this->fBelongsTo(User::class, 'user_id', 'id');\r\n }\r\n}\r\n\r\n\r\n//Find posts by a specific condition and get a collection\r\n$posts = Post::where(['post_id', '=', 'post1'])->get();\r\n\r\n//Access the post's author\r\n$posts->each(function ($post) {\r\n $author = $post->user();\r\n});\r\n\n"})})]})}function c(e={}){const{wrapper:n}={...(0,i.a)(),...e.components};return n?(0,r.jsx)(n,{...e,children:(0,r.jsx)(h,{...e})}):h(e)}},1151:(e,n,s)=>{s.d(n,{Z:()=>l,a:()=>t});var r=s(7294);const i={},o=r.createContext(i);function t(e){const n=r.useContext(o);return r.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function l(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(i):e.components||i:t(e.components),r.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.