{"id":3026,"date":"2020-07-30T05:37:05","date_gmt":"2020-07-30T05:37:05","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=3026"},"modified":"2020-07-30T06:45:53","modified_gmt":"2020-07-30T06:45:53","slug":"introduction-to-orm-in-laravel","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/introduction-to-orm-in-laravel\/","title":{"rendered":"Introduction to ORM in Laravel"},"content":{"rendered":"<p>Eloquent makes it very easy to interact with the application database with a new advanced technique for short queries using the model. ORM allows us to work with a database with short queries use objects and relationships between tables and it includes various methods to create, update and retrieve data from the database.<\/p>\n<h2>Eloquent ORM crud operations with example<\/h2>\n<ul>\n<li>\n<h3>To get data from the database from the database<\/h3>\n<ol>\n<li>\nWe need to create a model using the command. Always use table name in the plural form and the model name is in the singular form. For example:- Model name is \u2018Test\u2019 and the table name is \u2018Tests\u2019.<\/p>\n<p><strong>Php artisan make:model Test<\/strong><\/p>\n<p> \tusing below commands to create a migration<br \/>\n<strong>Php artisan make:model Test &#8211;migration<\/strong><br \/>\n<strong>Php artisan make:model Test  -m<\/strong><\/p>\n<p>The model should look like the following:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/07\/php1.png\" alt=\"php1\"\/><\/p>\n<p>For Example Tests Table:-<\/p>\n<table class=\"table table-striped\">\n<thead>\n<tr>\n<td>id<\/td>\n<td>title<\/td>\n<td>hits<\/td>\n<td>created_at<\/td>\n<td>updated_at<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>test1<\/td>\n<td>1<\/td>\n<td><small>2019-11-28 12:17:32.8933333<\/small><\/td>\n<td><small>2019-11-28 12:17:32.8933333<\/small><\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>test2<\/td>\n<td>0<\/td>\n<td><small>2019-11-28 12:19:32.8933333<\/small><\/td>\n<td><small>2019-11-28 12:19:32.8933333<\/small><\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>test3<\/td>\n<td>1<\/td>\n<td><small>2019-11-28 12:24:32.8933333<\/small><\/td>\n<td><small>2019-11-28 12:24:32.8933333<\/small><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/li>\n<li>\nCreate Controller using a command  like below<br \/>\n<strong>Php artisan make: controller TestController<\/strong><\/p>\n<p>->Create a retrieve method to get data from the database.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/07\/php2.png\" alt=\"php2\"\/><\/p>\n<p><strong>OUTPUT:<\/strong><\/p>\n<p>test1<br \/>\ntest2<br \/>\ntest3<\/p>\n<p>->Statement to get record from the database by the filter<\/p>\n<p><code><br \/>\n$result = App\\Test::where('hits', 1)<br \/>\n               ->orderBy('title', 'desc')<br \/>\n               ->take(2)<br \/>\n               ->get();<br \/>\n$jsonResult = $result->toJson();<br \/>\necho jsonResult;<\/p>\n<p><\/code><\/p>\n<p><strong>OUTPUT:<\/strong><br \/>\n[{&#8220;id&#8221;:&#8221;1,&#8221;title&#8221;:&#8221;test1&#8243;,&#8221;hits&#8221;:&#8221;1&#8243;,&#8221;created_at&#8221;:&#8221;2019-11-28 12:19:32.8933333&#8243;,\u201dupdated_at:\u201d2019-11-28 12:19:32.8933333\u201d},{&#8220;id&#8221;:&#8221;2,&#8221;title&#8221;:&#8221;test2&#8243;,&#8221;hits&#8221;:&#8221;0&#8243;,&#8221;created_at&#8221;:&#8221;2019-11-28 12:19:32.8933333&#8243;,\u201dupdated_at:\u201d2019-11-28 12:19:32.8933333\u201d}]\n<p>->Create an insert method to insert data from the database.<\/p>\n<p><code><br \/>\npublic function insert()<br \/>\n    {<br \/>\n\t\t$test = new Test();<br \/>\n\t\t$test->title = \u201ctest4\u201d;<br \/>\n$test->hits= 4;<br \/>\n$test->save();<br \/>\n    }<\/p>\n<p><\/code><\/p>\n<p><strong>OUTPUT:<\/strong><br \/>\nThe query will Insert new record as below<\/p>\n<table class=\"table table-striped\">\n<tbody>\n<tr>\n<td>4<\/td>\n<td>test4<\/td>\n<td>4<\/td>\n<td><small>2019-11-28 12:24:32.8933333<\/small><\/td>\n<td><small>2019-11-28 12:24:32.8933333<\/small><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>->Create an update method to update record into the database<br \/>\n<code><br \/>\npublic function update()<br \/>\n    {<br \/>\n\t\t$test = Test :: Find(1);<br \/>\n\t\t$test->title = \u201cTest update 1\u201d;<br \/>\n$test->hits= 2;<br \/>\n$test->save();<br \/>\n    }<\/p>\n<p><\/code><\/p>\n<p><strong>OUTPUT:<\/strong><\/p>\n<table class=\"table table-striped\">\n<thead>\n<tr>\n<td>id<\/td>\n<td>title<\/td>\n<td>hits<\/td>\n<td>created_at<\/td>\n<td>updated_at<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>Test update 1<\/td>\n<td>2<\/td>\n<td><small>2019-11-28 12:17:32.8933333<\/small><\/td>\n<td><small>2019-11-28 12:17:32.8933333<\/small><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>->Delete record from the table<br \/>\n<code><br \/>\n$test = Test::find(1);<br \/>\n$test->delete();<\/p>\n<p><\/code><br \/>\n<strong>OUTPUT:<\/strong><br \/>\nIt will delete the first row from the table<\/p>\n<h3>Collections<\/h3>\n<p>For <a href=\"https:\/\/studysection.com\/blog\/how-to-use-mongodb-with-laravel-eloquent-operations\/\">eloquent<\/a> methods for example \u2018all\u2019 and \u2018get\u2019, collections provide more functions to get the result in an easy way and we can also loop through collection results, for example,<br \/>\ncontains, diff, except, find, fresh, load, etc. Below is the example of find<br \/>\n<code><br \/>\n$result = Test::all();<br \/>\n$finalResult = $result->find(1);<br \/>\n$jsonResult = $finalResult->toJson();<br \/>\necho $jsonResult;<\/p>\n<p><\/code><\/p>\n<p><strong>OUTPUT:<\/strong><\/p>\n<p><em>[{&#8220;id&#8221;:&#8221;1,&#8221;title&#8221;:&#8221;test1&#8243;,&#8221;hits&#8221;:&#8221;1&#8243;,&#8221;created_at&#8221;:&#8221;2019-11-28 12:19:32.8933333&#8243;,\u201dupdated_at:\u201d2019-11-28 12:19:32.8933333\u201d}]<\/em>\n<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<h3>Chunking Results<\/h3>\n<p>If we want to retrieve thousands of record we can use the chunk method. It will return some of the records with pagination. For example<\/p>\n<p><code><br \/>\nPost::chunk(200, function ($result) {<br \/>\n    foreach ($result as $value) {<br \/>\n        \/\/<br \/>\n    }<br \/>\n});<\/p>\n<p><\/code><\/p>\n<p><strong>OUTPUT:<\/strong><br \/>\nIt will return 200 records first time with pagination and you can navigate to other pages by pagination.<\/p>\n<p><small><em>jQuery presents a tree-like structure of all the elements on a webpage simplifying the syntax and further manipulating such elements. The <a href=\"https:\/\/www.studysection.com\/jquery-3.x-expert\">jQuery Certification exam<\/a> by StudySection will secure your fundamental knowledge and a basic understanding of jQuery as an asset to improve your skills.<\/em><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Eloquent makes it very easy to interact with the application database with a new advanced technique for short queries using<\/p>\n","protected":false},"author":1,"featured_media":3034,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[65,191],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>StudySection Blog - Introduction to ORM in Laravel<\/title>\n<meta name=\"description\" content=\"ORM allows us to work with a database with short queries using objects and relationships between tables and it includes methods for managing the database.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/introduction-to-orm-in-laravel\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"StudySection Blog - Introduction to ORM in Laravel\" \/>\n<meta property=\"og:description\" content=\"ORM allows us to work with a database with short queries using objects and relationships between tables and it includes methods for managing the database.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/introduction-to-orm-in-laravel\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-30T05:37:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-07-30T06:45:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/07\/orm.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/introduction-to-orm-in-laravel\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/introduction-to-orm-in-laravel\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Introduction to ORM in Laravel\",\"datePublished\":\"2020-07-30T05:37:05+00:00\",\"dateModified\":\"2020-07-30T06:45:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/introduction-to-orm-in-laravel\/\"},\"wordCount\":445,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"laravel\",\"ORM\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/introduction-to-orm-in-laravel\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/introduction-to-orm-in-laravel\/\",\"url\":\"https:\/\/studysection.com\/blog\/introduction-to-orm-in-laravel\/\",\"name\":\"StudySection Blog - Introduction to ORM in Laravel\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2020-07-30T05:37:05+00:00\",\"dateModified\":\"2020-07-30T06:45:53+00:00\",\"description\":\"ORM allows us to work with a database with short queries using objects and relationships between tables and it includes methods for managing the database.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/introduction-to-orm-in-laravel\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/introduction-to-orm-in-laravel\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/introduction-to-orm-in-laravel\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Introduction to ORM in Laravel\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"StudySection Blog - Introduction to ORM in Laravel","description":"ORM allows us to work with a database with short queries using objects and relationships between tables and it includes methods for managing the database.","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:\/\/studysection.com\/blog\/introduction-to-orm-in-laravel\/","og_locale":"en_US","og_type":"article","og_title":"StudySection Blog - Introduction to ORM in Laravel","og_description":"ORM allows us to work with a database with short queries using objects and relationships between tables and it includes methods for managing the database.","og_url":"https:\/\/studysection.com\/blog\/introduction-to-orm-in-laravel\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2020-07-30T05:37:05+00:00","article_modified_time":"2020-07-30T06:45:53+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/07\/orm.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/introduction-to-orm-in-laravel\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/introduction-to-orm-in-laravel\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Introduction to ORM in Laravel","datePublished":"2020-07-30T05:37:05+00:00","dateModified":"2020-07-30T06:45:53+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/introduction-to-orm-in-laravel\/"},"wordCount":445,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["laravel","ORM"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/introduction-to-orm-in-laravel\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/introduction-to-orm-in-laravel\/","url":"https:\/\/studysection.com\/blog\/introduction-to-orm-in-laravel\/","name":"StudySection Blog - Introduction to ORM in Laravel","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2020-07-30T05:37:05+00:00","dateModified":"2020-07-30T06:45:53+00:00","description":"ORM allows us to work with a database with short queries using objects and relationships between tables and it includes methods for managing the database.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/introduction-to-orm-in-laravel\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/introduction-to-orm-in-laravel\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/introduction-to-orm-in-laravel\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Introduction to ORM in Laravel"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":198,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3026"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=3026"}],"version-history":[{"count":6,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3026\/revisions"}],"predecessor-version":[{"id":3037,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3026\/revisions\/3037"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/3034"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=3026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=3026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=3026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}