{"id":1119,"date":"2023-09-24T13:54:02","date_gmt":"2023-09-24T13:54:02","guid":{"rendered":"https:\/\/www.devopsfreelancer.com\/blog\/?p=1119"},"modified":"2023-09-30T13:59:23","modified_gmt":"2023-09-30T13:59:23","slug":"eloquent-orm-object-relational-mapping-in-laravel","status":"publish","type":"post","link":"https:\/\/www.devopsfreelancer.com\/blog\/eloquent-orm-object-relational-mapping-in-laravel\/","title":{"rendered":"Eloquent ORM (Object Relational Mapping) in Laravel"},"content":{"rendered":"\n<p>Eloquent is the ORM (Object-Relational Mapping) included with Laravel. It provides a simple and expressive way to interact with your database by representing database tables as PHP objects. This makes it easier to work with databases in a more natural and object-oriented way.<\/p>\n\n\n\n<p><strong>Create the Model:<\/strong><\/p>\n\n\n\n<p>In your terminal, run the following command to create a model named <code>Task<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan make:model Task -m\r<\/code><\/pre>\n\n\n\n<p>This will create a <code>Task.php<\/code> file in the <code>app<\/code> directory and a migration file for creating the corresponding database table.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>Define the Model<\/strong>:<\/li>\n<\/ol>\n\n\n\n<p>Open the <code>Task.php<\/code> file located in <code>app\/Models<\/code> (or <code>app<\/code> if you&#8217;re not using Laravel 8+ with separate Models directory), and define the model like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\r\n\r\nnamespace App\\Models;\r\n\r\nuse Illuminate\\Database\\Eloquent\\Factories\\HasFactory;\r\nuse Illuminate\\Database\\Eloquent\\Model;\r\n\r\nclass Task extends Model\r\n{\r\n    use HasFactory;\r\n\r\n    protected $fillable = &#91;'title', 'description']; \/\/ Specify which attributes can be mass assigned\r\n\r\n    \/\/ Additional methods or relationships can be defined here\r\n}\r<\/code><\/pre>\n\n\n\n<p>In this example, we&#8217;ve defined a <code>Task<\/code> model. It extends <code>Model<\/code>, which is the base Eloquent model provided by Laravel. The <code>HasFactory<\/code> trait is used to provide factory support for the model.<\/p>\n\n\n\n<p><strong>Create the Migration<\/strong>:<\/p>\n\n\n\n<p>In the <code>database\/migrations<\/code> directory, you&#8217;ll find a migration file created for the <code>tasks<\/code> table. Define the table&#8217;s schema in the <code>up()<\/code> method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public function up()\r\n{\r\n    Schema::create('tasks', function (Blueprint $table) {\r\n        $table->id();\r\n        $table->string('title');\r\n        $table->text('description');\r\n        $table->timestamps();\r\n    });\r\n}\r<\/code><\/pre>\n\n\n\n<p>Run the migration:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan migrate\r<\/code><\/pre>\n\n\n\n<p><strong>Using the Model<\/strong>:<\/p>\n\n\n\n<p>Now you can use the <code>Task<\/code> model to interact with the <code>tasks<\/code> table. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Create a new task\r\n$task = Task::create(&#91;\r\n    'title' => 'Sample Task',\r\n    'description' => 'This is a sample task description.'\r\n]);\r\n\r\n\/\/ Retrieve a task\r\n$task = Task::find(1);\r\n\r\n\/\/ Update a task\r\n$task->title = 'Updated Task Title';\r\n$task->save();\r\n\r\n\/\/ Delete a task\r\n$task->delete();\r\n<\/code><\/pre>\n\n\n\n<p>Thanks for reading <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Eloquent is the ORM (Object-Relational Mapping) included with Laravel. It provides a simple and expressive way to interact with your database by representing database tables as PHP&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[3,465,466,467],"class_list":["post-1119","post","type-post","status-publish","format-standard","hentry","category-laravel","tag-laravel","tag-laravel-orm","tag-object-relation-mapping","tag-orm"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Eloquent ORM (Object Relational Mapping) in Laravel - DevOps Freelancer<\/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.devopsfreelancer.com\/blog\/eloquent-orm-object-relational-mapping-in-laravel\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Eloquent ORM (Object Relational Mapping) in Laravel - DevOps Freelancer\" \/>\n<meta property=\"og:description\" content=\"Eloquent is the ORM (Object-Relational Mapping) included with Laravel. It provides a simple and expressive way to interact with your database by representing database tables as PHP...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.devopsfreelancer.com\/blog\/eloquent-orm-object-relational-mapping-in-laravel\/\" \/>\n<meta property=\"og:site_name\" content=\"DevOps Freelancer\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/amitsthakurs\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-09-24T13:54:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-09-30T13:59:23+00:00\" \/>\n<meta name=\"author\" content=\"Amit Kumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/amits_thakurs\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Amit Kumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/eloquent-orm-object-relational-mapping-in-laravel\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/eloquent-orm-object-relational-mapping-in-laravel\\\/\"},\"author\":{\"name\":\"Amit Kumar\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#\\\/schema\\\/person\\\/22ed4bd82dc04200a2ca541b3e35fc5b\"},\"headline\":\"Eloquent ORM (Object Relational Mapping) in Laravel\",\"datePublished\":\"2023-09-24T13:54:02+00:00\",\"dateModified\":\"2023-09-30T13:59:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/eloquent-orm-object-relational-mapping-in-laravel\\\/\"},\"wordCount\":184,\"commentCount\":1,\"keywords\":[\"laravel\",\"laravel-orm\",\"object-relation-mapping\",\"ORM\"],\"articleSection\":[\"Laravel\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/eloquent-orm-object-relational-mapping-in-laravel\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/eloquent-orm-object-relational-mapping-in-laravel\\\/\",\"url\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/eloquent-orm-object-relational-mapping-in-laravel\\\/\",\"name\":\"Eloquent ORM (Object Relational Mapping) in Laravel - DevOps Freelancer\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#website\"},\"datePublished\":\"2023-09-24T13:54:02+00:00\",\"dateModified\":\"2023-09-30T13:59:23+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#\\\/schema\\\/person\\\/22ed4bd82dc04200a2ca541b3e35fc5b\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/eloquent-orm-object-relational-mapping-in-laravel\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/eloquent-orm-object-relational-mapping-in-laravel\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/eloquent-orm-object-relational-mapping-in-laravel\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Eloquent ORM (Object Relational Mapping) in Laravel\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/\",\"name\":\"DevOps Freelancer\",\"description\":\"We provide DevOps | SRE | DevSecOps | MLOps Freelancing\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#\\\/schema\\\/person\\\/22ed4bd82dc04200a2ca541b3e35fc5b\",\"name\":\"Amit Kumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g\",\"caption\":\"Amit Kumar\"},\"description\":\"Hi I am Amit Kumar Thakur Experienced as s Software Developer with a demonstrated history of working in the information technology and services industry. Skilled in HTML, CSS, Bootstrap4, PHP, Laravel-9 , REST API,FB API,Google API, Youtube Api, Bitbucket,Github,Linux and jQuery. Strong engineering professional focused in Computer\\\/Information Technology Administration and Management. Currently my profile is to Software Developer, analyze the requirement, creating frame for web application, coding and maintenance.\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/amitsthakurs\\\/\",\"https:\\\/\\\/www.instagram.com\\\/amits_thakurs\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/amits-thakurs\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/amits_thakurs\"],\"url\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/author\\\/amit\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Eloquent ORM (Object Relational Mapping) in Laravel - DevOps Freelancer","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.devopsfreelancer.com\/blog\/eloquent-orm-object-relational-mapping-in-laravel\/","og_locale":"en_US","og_type":"article","og_title":"Eloquent ORM (Object Relational Mapping) in Laravel - DevOps Freelancer","og_description":"Eloquent is the ORM (Object-Relational Mapping) included with Laravel. It provides a simple and expressive way to interact with your database by representing database tables as PHP...","og_url":"https:\/\/www.devopsfreelancer.com\/blog\/eloquent-orm-object-relational-mapping-in-laravel\/","og_site_name":"DevOps Freelancer","article_author":"https:\/\/www.facebook.com\/amitsthakurs\/","article_published_time":"2023-09-24T13:54:02+00:00","article_modified_time":"2023-09-30T13:59:23+00:00","author":"Amit Kumar","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/amits_thakurs","twitter_misc":{"Written by":"Amit Kumar","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.devopsfreelancer.com\/blog\/eloquent-orm-object-relational-mapping-in-laravel\/#article","isPartOf":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/eloquent-orm-object-relational-mapping-in-laravel\/"},"author":{"name":"Amit Kumar","@id":"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b"},"headline":"Eloquent ORM (Object Relational Mapping) in Laravel","datePublished":"2023-09-24T13:54:02+00:00","dateModified":"2023-09-30T13:59:23+00:00","mainEntityOfPage":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/eloquent-orm-object-relational-mapping-in-laravel\/"},"wordCount":184,"commentCount":1,"keywords":["laravel","laravel-orm","object-relation-mapping","ORM"],"articleSection":["Laravel"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.devopsfreelancer.com\/blog\/eloquent-orm-object-relational-mapping-in-laravel\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.devopsfreelancer.com\/blog\/eloquent-orm-object-relational-mapping-in-laravel\/","url":"https:\/\/www.devopsfreelancer.com\/blog\/eloquent-orm-object-relational-mapping-in-laravel\/","name":"Eloquent ORM (Object Relational Mapping) in Laravel - DevOps Freelancer","isPartOf":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/#website"},"datePublished":"2023-09-24T13:54:02+00:00","dateModified":"2023-09-30T13:59:23+00:00","author":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b"},"breadcrumb":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/eloquent-orm-object-relational-mapping-in-laravel\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.devopsfreelancer.com\/blog\/eloquent-orm-object-relational-mapping-in-laravel\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.devopsfreelancer.com\/blog\/eloquent-orm-object-relational-mapping-in-laravel\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.devopsfreelancer.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Eloquent ORM (Object Relational Mapping) in Laravel"}]},{"@type":"WebSite","@id":"https:\/\/www.devopsfreelancer.com\/blog\/#website","url":"https:\/\/www.devopsfreelancer.com\/blog\/","name":"DevOps Freelancer","description":"We provide DevOps | SRE | DevSecOps | MLOps Freelancing","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.devopsfreelancer.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b","name":"Amit Kumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g","caption":"Amit Kumar"},"description":"Hi I am Amit Kumar Thakur Experienced as s Software Developer with a demonstrated history of working in the information technology and services industry. Skilled in HTML, CSS, Bootstrap4, PHP, Laravel-9 , REST API,FB API,Google API, Youtube Api, Bitbucket,Github,Linux and jQuery. Strong engineering professional focused in Computer\/Information Technology Administration and Management. Currently my profile is to Software Developer, analyze the requirement, creating frame for web application, coding and maintenance.","sameAs":["https:\/\/www.facebook.com\/amitsthakurs\/","https:\/\/www.instagram.com\/amits_thakurs\/","https:\/\/www.linkedin.com\/in\/amits-thakurs\/","https:\/\/x.com\/https:\/\/twitter.com\/amits_thakurs"],"url":"https:\/\/www.devopsfreelancer.com\/blog\/author\/amit\/"}]}},"_links":{"self":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/comments?post=1119"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1119\/revisions"}],"predecessor-version":[{"id":1120,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1119\/revisions\/1120"}],"wp:attachment":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/media?parent=1119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/categories?post=1119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/tags?post=1119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}