{"id":654,"date":"2023-08-26T09:39:03","date_gmt":"2023-08-26T09:39:03","guid":{"rendered":"https:\/\/www.devopssupport.in\/blog\/?p=654"},"modified":"2023-09-01T04:33:18","modified_gmt":"2023-09-01T04:33:18","slug":"how-to-use-the-eloquent-orm-object-relational-mapping","status":"publish","type":"post","link":"https:\/\/www.devopssupport.in\/blog\/how-to-use-the-eloquent-orm-object-relational-mapping\/","title":{"rendered":"How to use the Eloquent ORM (Object-Relational Mapping)"},"content":{"rendered":"\n<p>In Laravel, you can use the Eloquent ORM (Object-Relational Mapping) to interact with your database using a fluent and expressive query builder. The query builder provides a set of methods that allow you to construct and execute database queries in a convenient and efficient manner. <\/p>\n\n\n\n<p>Here are some basic queries commonly used in Laravel, along with examples:<\/p>\n\n\n\n<p><strong>Retrieving All Records:<\/strong><\/p>\n\n\n\n<p>To retrieve all records from a table, you can use the <code>all<\/code> method. Here&#8217;s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$users = App\\Models\\User::all();\r<\/code><\/pre>\n\n\n\n<p>This query retrieves all records from the <code>users<\/code> table and assigns them to the <code>$users<\/code> variable.<\/p>\n\n\n\n<p><strong>Retrieving Specific Columns:<\/strong><\/p>\n\n\n\n<p>If you only need specific columns from a table, you can use the <code>select<\/code> method. <\/p>\n\n\n\n<p>Here&#8217;s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$users = App\\Models\\User::select('name', 'email')->get();\r\n<\/code><\/pre>\n\n\n\n<p>This query retrieves the <code>name<\/code> and <code>email<\/code> columns from the <code>users<\/code> table.<\/p>\n\n\n\n<p><strong>Retrieving a Single Record:<\/strong><\/p>\n\n\n\n<p>To retrieve a single record based on a condition, you can use the <code>where<\/code> method. Here&#8217;s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$user = App\\Models\\User::where('id', 1)->first();\r\n<\/code><\/pre>\n\n\n\n<p>This query retrieves the user with an <code>id<\/code> of 1 from the <code>users<\/code> table.<\/p>\n\n\n\n<p><strong>Retrieving Records with Conditions:<\/strong><\/p>\n\n\n\n<p>You can add more conditions to your query using the <code>where<\/code> method. <\/p>\n\n\n\n<p>Here&#8217;s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$users = App\\Models\\User::where('age', '>', 18)\r\n                ->where('gender', 'female')\r\n                ->get();\r\n<\/code><\/pre>\n\n\n\n<p>This query retrieves all female users over the age of 18 from the <code>users<\/code> table.<\/p>\n\n\n\n<p><strong>Ordering Records:<\/strong><\/p>\n\n\n\n<p>To order records based on a specific column, you can use the <code>orderBy<\/code> method. <\/p>\n\n\n\n<p>Here&#8217;s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$users = App\\Models\\User::orderBy('name', 'asc')->get();\r\n<\/code><\/pre>\n\n\n\n<p>This query retrieves all users from the <code>users<\/code> table and orders them in ascending order based on the <code>name<\/code> column.<\/p>\n\n\n\n<p><strong>Counting Records:<\/strong><\/p>\n\n\n\n<p>To count the number of records that match certain conditions, you can use the <code>count<\/code> method. Here&#8217;s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$count = App\\Models\\User::where('status', 'active')->count();\r\n<\/code><\/pre>\n\n\n\n<p>This query counts the number of active users in the <code>users<\/code> table.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Laravel, you can use the Eloquent ORM (Object-Relational Mapping) to interact with your database using a fluent and expressive query builder. The query builder provides a&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-654","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/654","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/comments?post=654"}],"version-history":[{"count":1,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/654\/revisions"}],"predecessor-version":[{"id":655,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/654\/revisions\/655"}],"wp:attachment":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/media?parent=654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/categories?post=654"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/tags?post=654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}