{"id":414,"date":"2023-05-30T05:54:50","date_gmt":"2023-05-30T05:54:50","guid":{"rendered":"https:\/\/www.devopsconsulting.in\/blog\/?p=414"},"modified":"2023-05-30T05:55:39","modified_gmt":"2023-05-30T05:55:39","slug":"eloquent-relationships-in-laravel-with-example","status":"publish","type":"post","link":"https:\/\/www.devopsconsulting.in\/blog\/eloquent-relationships-in-laravel-with-example\/","title":{"rendered":"Eloquent: Relationships in Laravel with example"},"content":{"rendered":"\n<p>Laravel framework, relationships refer to the associations established between database tables\/models. Laravel provides a convenient and expressive syntax for defining and managing these relationships, making it easier to work with related data.<\/p>\n\n\n\n<p><strong>Laravel offers several types of relationships:<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>One-to-One Relationship: <\/strong><\/h2>\n\n\n\n<p>This type of relationship exists when a single record in one table\/model is associated with a single record in another table\/model. For example, a User model may have a one-to-one relationship with a Profile model, where each user has one profile associated with them.<\/p>\n\n\n\n<p>Consider the scenario where we have a User model and a Profile model. Each user has one profile associated with them.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ In the User model\n\nclass User extends Model\n{\n    public function profile()\n    {\n        return $this-&gt;hasOne(Profile::class);\n    }\n}\n\n\/\/ In the Profile model\n\nclass Profile extends Model\n{\n    public function user()\n    {\n        return $this-&gt;belongsTo(User::class);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>With these relationships defined, you can access the profile of a user like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$user = User::find(1);\n$profile = $user-&gt;profile;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>One-to-Many Relationship:<\/strong><\/h2>\n\n\n\n<p>In a one-to-many relationship, a single record in one table\/model is associated with multiple records in another table\/model. For instance, a User model may have a one-to-many relationship with a Post model, where a user can have multiple posts.<\/p>\n\n\n\n<p>Let&#8217;s consider a scenario where we have a User model and a Post model. A user can have multiple posts.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ In the User model\n\nclass User extends Model\n{\n    public function posts()\n    {\n        return $this-&gt;hasMany(Post::class);\n    }\n}\n\n\/\/ In the Post model:\n\nclass Post extends Model\n{\n    public function user()\n    {\n        return $this-&gt;belongsTo(User::class);\n    }\n}<\/code><\/pre>\n\n\n\n<p>You can retrieve all posts belonging to a user like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$user = User::find(1);\n$posts = $user-&gt;posts;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Many-to-Many Relationship:<\/strong><\/h2>\n\n\n\n<p>Many-to-many relationships occur when multiple records in one table\/model are associated with multiple records in another table\/model. For example, a User model may have a many-to-many relationship with a Role model, where users can have multiple roles and roles can be assigned to multiple users.<\/p>\n\n\n\n<p>Let&#8217;s consider a scenario where we have a User model and a Role model. Users can have multiple roles, and roles can be assigned to multiple users.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ In the User model: \n\nclass User extends Model\n{\npublic function roles()\n{\nreturn $this-&gt;belongsToMany(Role::class);\n}\n}\n\n\/\/ In the Role model:\n\nclass Role extends Model\n{\n    public function users()\n    {\n        return $this-&gt;belongsToMany(User::class);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>You can retrieve all roles associated with a user like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$user = User::find(1);\n$roles = $user-&gt;roles;\n<\/code><\/pre>\n\n\n\n<p>These are just a few examples of the relationships you can define in Laravel using eloquent. Laravel provides a rich set of methods and conventions to work with relationships, allowing you to easily navigate and manipulate associated data in your applications.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Laravel framework, relationships refer to the associations established between database tables\/models. Laravel provides a convenient and expressive syntax for defining and managing these relationships, making it easier&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[87],"tags":[88],"class_list":["post-414","post","type-post","status-publish","format-standard","hentry","category-laravel","tag-eloquent-relationships-in-laravel"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Eloquent: Relationships in Laravel with example - DevOps Consulting<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.devopsconsulting.in\/blog\/eloquent-relationships-in-laravel-with-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Eloquent: Relationships in Laravel with example - DevOps Consulting\" \/>\n<meta property=\"og:description\" content=\"Laravel framework, relationships refer to the associations established between database tables\/models. Laravel provides a convenient and expressive syntax for defining and managing these relationships, making it easier...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.devopsconsulting.in\/blog\/eloquent-relationships-in-laravel-with-example\/\" \/>\n<meta property=\"og:site_name\" content=\"DevOps Consulting\" \/>\n<meta property=\"article:published_time\" content=\"2023-05-30T05:54:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-05-30T05:55:39+00:00\" \/>\n<meta name=\"author\" content=\"Abhishek Singh\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Abhishek Singh\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/eloquent-relationships-in-laravel-with-example\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/eloquent-relationships-in-laravel-with-example\\\/\"},\"author\":{\"name\":\"Abhishek Singh\",\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/#\\\/schema\\\/person\\\/fc397ba8be42f9fdd53450edfc73006f\"},\"headline\":\"Eloquent: Relationships in Laravel with example\",\"datePublished\":\"2023-05-30T05:54:50+00:00\",\"dateModified\":\"2023-05-30T05:55:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/eloquent-relationships-in-laravel-with-example\\\/\"},\"wordCount\":332,\"commentCount\":1,\"keywords\":[\"Eloquent: Relationships in Laravel\"],\"articleSection\":[\"Laravel\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/eloquent-relationships-in-laravel-with-example\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/eloquent-relationships-in-laravel-with-example\\\/\",\"url\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/eloquent-relationships-in-laravel-with-example\\\/\",\"name\":\"Eloquent: Relationships in Laravel with example - DevOps Consulting\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/#website\"},\"datePublished\":\"2023-05-30T05:54:50+00:00\",\"dateModified\":\"2023-05-30T05:55:39+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/#\\\/schema\\\/person\\\/fc397ba8be42f9fdd53450edfc73006f\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/eloquent-relationships-in-laravel-with-example\\\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/\",\"name\":\"DevOps Consulting\",\"description\":\"DevOps Consulting | SRE Consulting | DevSecOps Consulting | MLOps Consulting\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/#\\\/schema\\\/person\\\/fc397ba8be42f9fdd53450edfc73006f\",\"name\":\"Abhishek Singh\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/790feefe779852cdf344ca7318bf6c13832223c9b3c6bf4d217658412041026d?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/790feefe779852cdf344ca7318bf6c13832223c9b3c6bf4d217658412041026d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/790feefe779852cdf344ca7318bf6c13832223c9b3c6bf4d217658412041026d?s=96&d=mm&r=g\",\"caption\":\"Abhishek Singh\"},\"description\":\"I\u2019m Abhishek, a DevOps, SRE, DevSecOps, and Cloud expert with a passion for sharing knowledge and real-world experiences. I\u2019ve had the opportunity to work with Cotocus and continue to contribute to multiple platforms where I share insights across different domains: \u2022 DevOps School \u2013 Tech blogs and tutorials \u2022 Holiday Landmark \u2013 Travel stories and guides \u2022 Stocks Mantra \u2013 Stock market strategies and tips \u2022 My Medic Plus \u2013 Health and fitness guidance \u2022 TrueReviewNow \u2013 Honest product reviews \u2022 Wizbrand \u2013 SEO and digital tools for businesses I\u2019m also exploring the fascinating world of Quantum Computing.\",\"url\":\"https:\\\/\\\/www.devopsconsulting.in\\\/blog\\\/author\\\/abhishek\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Eloquent: Relationships in Laravel with example - DevOps Consulting","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.devopsconsulting.in\/blog\/eloquent-relationships-in-laravel-with-example\/","og_locale":"en_US","og_type":"article","og_title":"Eloquent: Relationships in Laravel with example - DevOps Consulting","og_description":"Laravel framework, relationships refer to the associations established between database tables\/models. Laravel provides a convenient and expressive syntax for defining and managing these relationships, making it easier...","og_url":"https:\/\/www.devopsconsulting.in\/blog\/eloquent-relationships-in-laravel-with-example\/","og_site_name":"DevOps Consulting","article_published_time":"2023-05-30T05:54:50+00:00","article_modified_time":"2023-05-30T05:55:39+00:00","author":"Abhishek Singh","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Abhishek Singh","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.devopsconsulting.in\/blog\/eloquent-relationships-in-laravel-with-example\/#article","isPartOf":{"@id":"https:\/\/www.devopsconsulting.in\/blog\/eloquent-relationships-in-laravel-with-example\/"},"author":{"name":"Abhishek Singh","@id":"https:\/\/www.devopsconsulting.in\/blog\/#\/schema\/person\/fc397ba8be42f9fdd53450edfc73006f"},"headline":"Eloquent: Relationships in Laravel with example","datePublished":"2023-05-30T05:54:50+00:00","dateModified":"2023-05-30T05:55:39+00:00","mainEntityOfPage":{"@id":"https:\/\/www.devopsconsulting.in\/blog\/eloquent-relationships-in-laravel-with-example\/"},"wordCount":332,"commentCount":1,"keywords":["Eloquent: Relationships in Laravel"],"articleSection":["Laravel"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.devopsconsulting.in\/blog\/eloquent-relationships-in-laravel-with-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.devopsconsulting.in\/blog\/eloquent-relationships-in-laravel-with-example\/","url":"https:\/\/www.devopsconsulting.in\/blog\/eloquent-relationships-in-laravel-with-example\/","name":"Eloquent: Relationships in Laravel with example - DevOps Consulting","isPartOf":{"@id":"https:\/\/www.devopsconsulting.in\/blog\/#website"},"datePublished":"2023-05-30T05:54:50+00:00","dateModified":"2023-05-30T05:55:39+00:00","author":{"@id":"https:\/\/www.devopsconsulting.in\/blog\/#\/schema\/person\/fc397ba8be42f9fdd53450edfc73006f"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.devopsconsulting.in\/blog\/eloquent-relationships-in-laravel-with-example\/"]}]},{"@type":"WebSite","@id":"https:\/\/www.devopsconsulting.in\/blog\/#website","url":"https:\/\/www.devopsconsulting.in\/blog\/","name":"DevOps Consulting","description":"DevOps Consulting | SRE Consulting | DevSecOps Consulting | MLOps Consulting","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.devopsconsulting.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.devopsconsulting.in\/blog\/#\/schema\/person\/fc397ba8be42f9fdd53450edfc73006f","name":"Abhishek Singh","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/790feefe779852cdf344ca7318bf6c13832223c9b3c6bf4d217658412041026d?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/790feefe779852cdf344ca7318bf6c13832223c9b3c6bf4d217658412041026d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/790feefe779852cdf344ca7318bf6c13832223c9b3c6bf4d217658412041026d?s=96&d=mm&r=g","caption":"Abhishek Singh"},"description":"I\u2019m Abhishek, a DevOps, SRE, DevSecOps, and Cloud expert with a passion for sharing knowledge and real-world experiences. I\u2019ve had the opportunity to work with Cotocus and continue to contribute to multiple platforms where I share insights across different domains: \u2022 DevOps School \u2013 Tech blogs and tutorials \u2022 Holiday Landmark \u2013 Travel stories and guides \u2022 Stocks Mantra \u2013 Stock market strategies and tips \u2022 My Medic Plus \u2013 Health and fitness guidance \u2022 TrueReviewNow \u2013 Honest product reviews \u2022 Wizbrand \u2013 SEO and digital tools for businesses I\u2019m also exploring the fascinating world of Quantum Computing.","url":"https:\/\/www.devopsconsulting.in\/blog\/author\/abhishek\/"}]}},"_links":{"self":[{"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/posts\/414","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/comments?post=414"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/posts\/414\/revisions"}],"predecessor-version":[{"id":417,"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/posts\/414\/revisions\/417"}],"wp:attachment":[{"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/media?parent=414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/categories?post=414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsconsulting.in\/blog\/wp-json\/wp\/v2\/tags?post=414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}