PageSourceSearch

https://firestore-eloquent.netlify.app/assets/js/ffef71b7.af23e885.js

js firestore-eloquent.netlify.app collected 2026-10-03 10:44:33 UTC 14,774 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunkfirestore_eloquent=self.webpackChunkfirestore_eloquent||[]).push([[2804],{5255:(e,n,s)=>{s.r(n),s.d(n,{assets:()=>o,contentTitle:()=>l,default:()=>c,frontMatter:()=>d,metadata:()=>a,toc:()=>t});var i=s(5893),r=s(1151);const d={sidebar_position:6},l="Relationships",a={id:"guide/eloquent/relationships",title:"Relationships",description:"`hasOne` Relationship",source:"@site/versioned_docs/version-1.x/guide/eloquent/relationships.mdx",sourceDirName:"guide/eloquent",slug:"/guide/eloquent/relationships",permalink:"/docs/1.x/guide/eloquent/relationships",draft:!1,unlisted:!1,editUrl:"https://github.com/FreddyWhest/firestore-eloquent-docs/tree/main/versioned_docs/version-1.x/guide/eloquent/relationships.mdx",tags:[],version:"1.x",sidebarPosition:6,frontMatter:{sidebar_position:6},sidebar:"tutorialSidebar",previous:{title:"Deleting Documents",permalink:"/docs/1.x/guide/eloquent/deleting-data"},next:{title:"Pagination",permalink:"/docs/1.x/guide/eloquent/pagination"}},o={},t=[{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",id:"example-1-finding-a-user-and-their-profile",level:3},{value:"Using <code>find()</code> method:",id:"using-find-method",level:4},{value:"Alternative Using Model Class:",id:"alternative-using-model-class",level:4},{value:"Example 2: Retrieving User and Profile with <code>first()</code> and <code>firstOrFail()</code>",id:"example-2-retrieving-user-and-profile-with-first-and-firstorfail",level:3},{value:"Using <code>first()</code> method:",id:"using-first-method",level:4},{value:"Using <code>firstOrFail()</code> method:",id:"using-firstorfail-method",level:4},{value:"Example 3: Retrieving Users and Their Profiles with get(), all(), and paginate()",id:"example-3-retrieving-users-and-their-profiles-with-get-all-and-paginate",level:3},{value:"Using <code>get()</code> method:",id:"using-get-method",level:4},{value:"Using <code>all()</code> method:",id:"using-all-method",level:4},{value:"Using <code>paginate()</code> method:",id:"using-paginate-method",level:4},{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:"Using <code>find()</code> method:",id:"using-find-method-1",level:4},{value:"Example 3: Retrieving Users and Their Posts with <code>get()</code>, <code>all()</code>, and <code>paginate()</code>",id:"example-3-retrieving-users-and-their-posts-with-get-all-and-paginate",level:4},{value:"Using <code>get()</code> method:",id:"using-get-method-1",level:4},{value:"Using <code>all()</code> method:",id:"using-all-method-1",level:4},{value:"Using <code>paginate()</code> method:",id:"using-paginate-method-1",level:4}];function h(e){const n={code:"code",h1:"h1",h2:"h2",h3:"h3",h4:"h4",li:"li",p:"p",pre:"pre",strong:"strong",ul:"ul",...(0,r.a)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(n.h1,{id:"relationships",children:"Relationships"}),"\n",(0,i.jsxs)(n.h2,{id:"hasone-relationship",children:[(0,i.jsx)(n.code,{children:"hasOne"})," Relationship"]}),"\n",(0,i.jsxs)(n.p,{children:["The ",(0,i.jsx)(n.code,{children:"hasOne"})," relationship is used to define a one-to-one relationship between two models. For example, a ",(0,i.jsx)(n.code,{children:"User"})," model might have one ",(0,i.jsx)(n.code,{children:"Phone"})," model:"]}),"\n",(0,i.jsxs)(n.p,{children:["Defining a ",(0,i.jsx)(n.strong,{children:(0,i.jsx)(n.code,{children:"hasOne"})})," Relationship Let's explore the components of defining a hasOne relationship:"]}),"\n",(0,i.jsx)(n.p,{children:(0,i.jsx)(n.code,{children:"hasOne($model, $foreignKey, $localKey)"})}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsxs)(n.li,{children:["\n",(0,i.jsxs)(n.p,{children:[(0,i.jsxs)(n.strong,{children:[(0,i.jsx)(n.code,{children:"$model"}),":"]})," The related model class name (e.g., ",(0,i.jsx)(n.strong,{children:"'Profile'"}),"). This is the class of the model that the current model (User) is related to."]}),"\n"]}),"\n",(0,i.jsxs)(n.li,{children:["\n",(0,i.jsxs)(n.p,{children:[(0,i.jsxs)(n.strong,{children:[(0,i.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,i.jsx)(n.code,{children:"user_id"})," that references the id column in the User model, you would specify ",(0,i.jsx)(n.code,{children:"'user_id'"})," as the ",(0,i.jsx)(n.code,{children:"$foreignKey"}),"."]}),"\n"]}),"\n",(0,i.jsxs)(n.li,{children:["\n",(0,i.jsxs)(n.p,{children:[(0,i.jsxs)(n.strong,{children:[(0,i.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,i.jsx)(n.h3,{id:"accessing-the-related-model",children:"Accessing the Related Model"}),"\n",(0,i.jsx)(n.p,{children:"Once the relationship is defined, you can access the related model using the hasOne relationship."}),"\n",(0,i.jsx)(n.h3,{id:"example-1-finding-a-user-and-their-profile",children:"Example 1: Finding a User and their Profile"}),"\n",(0,i.jsxs)(n.h4,{id:"using-find-method",children:["Using ",(0,i.jsx)(n.code,{children:"find()"})," method:"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-php",children:"// Find a user by ID\r\n$user = User::find('user1');\r\n\r\n// Access the user's profile\r\n$profile = $user->hasOne('Profile', 'user_id', 'id');\r\n\n"})}),"\n",(0,i.jsx)(n.h4,{id:"alternative-using-model-class",children:"Alternative Using Model Class:"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-php",children:"use App\\FModel\\Profile;\r\n\r\n// Find a user by ID\r\n$user = User::find('user1');\r\n\r\n// Access the user's profile using the Profile model class\r\n$profile = $user->hasOne(Profile::class, 'user_id', 'id');\n"})}),"\n",(0,i.jsxs)(n.h3,{id:"example-2-retrieving-user-and-profile-with-first-and-firstorfail",children:["Example 2: Retrieving User and Profile with ",(0,i.jsx)(n.code,{children:"first()"})," and ",(0,i.jsx)(n.code,{children:"firstOrFail()"})]}),"\n",(0,i.jsxs)(n.h4,{id:"using-first-method",children:["Using ",(0,i.jsx)(n.code,{children:"first()"})," method:"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-php",children:"// Find a user by a specific condition\r\n$user = User::where(['user_id', '=', 'user1'])->first();\r\n\r\n// Access the user's profile\r\n$profile = $user->hasOne('Profile', 'user_id', 'id');\n"})}),"\n",(0,i.jsxs)(n.h4,{id:"using-firstorfail-method",children:["Using ",(0,i.jsx)(n.code,{children:"firstOrFail()"})," method:"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-php",children:"// Find a user by a specific condition\r\n$user = User::where(['user_id', '=', 'user1'])->firstOrFail();\r\n\r\n// Access the user's profile\r\n$profile = $user->hasOne('Profile', 'user_id', 'id');\n"})}),"\n",(0,i.jsx)(n.h3,{id:"example-3-retrieving-users-and-their-profiles-with-get-all-and-paginate",children:"Example 3: Retrieving Users and Their Profiles with get(), all(), and paginate()"}),"\n",(0,i.jsxs)(n.h4,{id:"using-get-method",children:["Using ",(0,i.jsx)(n.code,{children:"get()"})," method:"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-php",children:"// Find users by a specific condition and get a collection\r\n$users = User::where(['user_id', '=', 'user1'])->get();\r\n\r\n// Display user information in Blade\r\n@foreach ($users as $user)\r\n    <h1>{{ $user->hasOne('Profile', 'user_id', 'id')->email }}</h1>\r\n@endforeach\n"})}),"\n",(0,i.jsxs)(n.h4,{id:"using-all-method",children:["Using ",(0,i.jsx)(n.code,{children:"all()"})," method:"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-php",children:"// Get all users\r\n$users = User::all();\r\n\r\n// Display user information in Blade\r\n@foreach ($users as $user)\r\n    <h1>{{ $user->hasOne('Profile', 'user_id', 'id')->email }}</h1>\r\n@endforeach\n"})}),"\n",(0,i.jsxs)(n.h4,{id:"using-paginate-method",children:["Using ",(0,i.jsx)(n.code,{children:"paginate()"})," method:"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-php",children:"// Get all users\r\nuse App\\FModel\\Profile;\r\n\r\n// Find users by a specific condition and paginate the results\r\n$users = User::where(['user_id', '=', 'user1'])->paginate(10);\r\n\r\n// Display user information in Blade\r\n@foreach ($users->data() as $user)\r\n    <h1>{{ $user->hasOne(Profile::class, 'user_id', 'id')->email }}</h1>\r\n@endforeach\n"})}),"\n",(0,i.jsxs)(n.h2,{id:"hasmany-relationship",children:[(0,i.jsx)(n.code,{children:"hasMany"})," Relationship"]}),"\n",(0,i.jsxs)(n.p,{children:["In Eloquent, the ",(0,i.jsx)(n.strong,{children:(0,i.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,i.jsx)(n.strong,{children:"User"}),") can have multiple related records in another model (e.g., ",(0,i.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,i.jsxs)(n.h3,{id:"defining-a-hasmany-relationship",children:["Defining a ",(0,i.jsx)(n.code,{children:"hasMany"})," Relationship"]}),"\n",(0,i.jsx)(n.p,{children:"Let's explore the components of defining a hasMany relationship:"}),"\n",(0,i.jsx)(n.p,{children:(0,i.jsx)(n.code,{children:"hasMany($model, $foreignKey, $localKey)"})}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsxs)(n.li,{children:["\n",(0,i.jsxs)(n.p,{children:[(0,i.jsxs)(n.strong,{children:[(0,i.jsx)(n.code,{children:"$model"}),":"]})," The related model class name (e.g., ",(0,i.jsx)(n.strong,{children:"'Post'"}),"). This is the class of the model that the current model (User) is related to."]}),"\n"]}),"\n",(0,i.jsxs)(n.li,{children:["\n",(0,i.jsxs)(n.p,{children:[(0,i.jsxs)(n.strong,{children:[(0,i.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,i.jsx)(n.code,{children:"user_id"})," that references the id column in the User model, you would specify ",(0,i.jsx)(n.code,{children:"'user_id'"})," as the ",(0,i.jsx)(n.code,{children:"$foreignKey"}),"."]}),"\n"]}),"\n",(0,i.jsxs)(n.li,{children:["\n",(0,i.jsxs)(n.p,{children:[(0,i.jsxs)(n.strong,{children:[(0,i.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,i.jsx)(n.h3,{id:"accessing-the-related-model-1",children:"Accessing the Related Model"}),"\n",(0,i.jsx)(n.p,{children:"Once the relationship is defined, you can access the related model using the hasMany relationship."}),"\n",(0,i.jsx)(n.h3,{id:"example-1-finding-a-user-and-their-posts",children:"Example 1: Finding a User and their Posts"}),"\n",(0,i.jsxs)(n.h4,{id:"using-find-method-1",children:["Using ",(0,i.jsx)(n.code,{children:"find()"})," method:"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-php",children:"// Find a user by ID\r\n$user = User::find('user1');\r\n\r\n// Access the user's profile\r\n$posts = $user->hasMany('Post', 'user_id', 'id');\r\n\r\n//Blade rendering\r\n@foreach ($posts as $post)\r\n    <h1>{{ $post->title }}</h1>\r\n@endforeach\n"})}),"\n",(0,i.jsxs)(n.h4,{id:"example-3-retrieving-users-and-their-posts-with-get-all-and-paginate",children:["Example 3: Retrieving Users and Their Posts with ",(0,i.jsx)(n.code,{children:"get()"}),", ",(0,i.jsx)(n.code,{children:"all()"}),", and ",(0,i.jsx)(n.code,{children:"paginate()"})]}),"\n",(0,i.jsxs)(n.h4,{id:"using-get-method-1",children:["Using ",(0,i.jsx)(n.code,{children:"get()"})," method:"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-php",children:"// Find users by a specific condition and get a collection\r\n$users = User::where(['user_id', '=', 'user1'])->get();\r\n\r\n// Display user information in Blade\r\n@foreach ($users as $user)\r\n    @php\r\n        $posts = $user->hasMany('Profile', 'user_id', 'id') \r\n    @endphp\r\n\r\n    @foreach ($posts as $post)\r\n        <h1>{{ $post->title }}</h1>\r\n    @endforeach\r\n\r\n@endforeach\n"})}),"\n",(0,i.jsxs)(n.h4,{id:"using-all-method-1",children:["Using ",(0,i.jsx)(n.code,{children:"all()"})," method:"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-php",children:"// Get all users\r\n$users = User::all();\r\n\r\n// Display user information in Blade\r\n@foreach ($users as $user)\r\n    @php\r\n        $posts = $user->hasMany('Profile', 'user_id', 'id') \r\n    @endphp\r\n\r\n    @foreach ($posts as $post)\r\n        <h1>{{ $post->title }}</h1>\r\n    @endforeach\r\n\r\n@endforeach\n"})}),"\n",(0,i.jsxs)(n.h4,{id:"using-paginate-method-1",children:["Using ",(0,i.jsx)(n.code,{children:"paginate()"})," method:"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-php",children:"// Get all users\r\nuse App\\FModel\\Profile;\r\n\r\n// Find users by a specific condition and paginate the results\r\n$users = User::where(['user_id', '=', 'user1'])->paginate(10);\r\n\r\n// Display user information in Blade\r\n@foreach ($users->data() as $user)\r\n    @php\r\n        $posts = $user->hasMany(Profile::class, 'user_id', 'id') \r\n    @endphp\r\n\r\n    @foreach ($posts as $post)\r\n        <h1>{{ $post->title }}</h1>\r\n    @endforeach\r\n\r\n@endforeach\n"})})]})}function c(e={}){const{wrapper:n}={...(0,r.a)(),...e.components};return n?(0,i.jsx)(n,{...e,children:(0,i.jsx)(h,{...e})}):h(e)}},1151:(e,n,s)=>{s.d(n,{Z:()=>a,a:()=>l});var i=s(7294);const r={},d=i.createContext(r);function l(e){const n=i.useContext(d);return i.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function a(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(r):e.components||r:l(e.components),i.createElement(d.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.