{"id":167,"date":"2026-05-20T15:55:53","date_gmt":"2026-05-20T15:55:53","guid":{"rendered":"https:\/\/gigz.pk\/php\/?post_type=lesson&#038;p=167"},"modified":"2026-05-21T14:43:01","modified_gmt":"2026-05-21T14:43:01","slug":"eloquent-orm","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/php\/?lesson=eloquent-orm","title":{"rendered":"Eloquent ORM"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction to Eloquent ORM<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Eloquent ORM is Laravel\u2019s built-in Object Relational Mapping system that allows developers to interact with databases using PHP syntax instead of writing complex SQL queries.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Eloquent makes database operations simple, readable, and efficient by representing database tables as models and records as objects.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is one of the most powerful features of the Laravel framework and is widely used for modern web application development.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Objectives<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By the end of this training, you will be able to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Understand the concept of ORM<\/li>\n\n\n\n<li>Learn how Eloquent ORM works in Laravel<\/li>\n\n\n\n<li>Create and manage models<\/li>\n\n\n\n<li>Perform database operations using Eloquent<\/li>\n\n\n\n<li>Work with relationships between tables<\/li>\n\n\n\n<li>Use migrations with Eloquent models<\/li>\n\n\n\n<li>Build secure and scalable applications<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What is ORM<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">ORM stands for Object Relational Mapping.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is a programming technique that connects database tables with object-oriented programming languages.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Instead of writing SQL queries manually, developers can use PHP classes and methods to interact with the database.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Traditional SQL Query:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM users;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Eloquent ORM Query:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$users = User::all();<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Features of Eloquent ORM<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Simple Database Interaction<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Eloquent allows developers to insert, update, delete, and retrieve data using clean syntax.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Active Record Pattern<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Each database table is connected to a model class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Built-in Relationships<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Eloquent supports one-to-one, one-to-many, many-to-many, and polymorphic relationships.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Query Builder Integration<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Eloquent includes Laravel\u2019s powerful query builder for advanced database queries.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Secure and Efficient<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It helps protect applications from SQL injection attacks through parameter binding.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating an Eloquent Model<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Models represent database tables in Laravel.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Command to create a model:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan make:model User<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Example Model:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br><br>namespace App\\Models;<br><br>use Illuminate\\Database\\Eloquent\\Model;<br><br>class User extends Model<br>{<br>    protected $table = 'users';<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Database Configuration<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Database settings are stored in the <code>.env<\/code> file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DB_CONNECTION=mysql<br>DB_HOST=127.0.0.1<br>DB_PORT=3306<br>DB_DATABASE=training_db<br>DB_USERNAME=root<br>DB_PASSWORD=<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Retrieving Data with Eloquent<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Get All Records<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$users = User::all();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Find Record by ID<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$user = User::find(1);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Retrieve First Record<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$user = User::first();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Using Conditions<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$users = User::where('status', 'active')-&gt;get();<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Inserting Data<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>$user = new User();<br>$user-&gt;name = 'Ali';<br>$user-&gt;email = 'ali@example.com';<br>$user-&gt;save();<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Updating Data<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>$user = User::find(1);<br>$user-&gt;name = 'Ahmed';<br>$user-&gt;save();<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Deleting Data<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>$user = User::find(1);<br>$user-&gt;delete();<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Mass Assignment<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Eloquent allows mass assignment for inserting multiple fields.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class User extends Model<br>{<br>    protected $fillable = &#91;'name', 'email'];<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>User::create(&#91;<br>    'name' =&gt; 'Ali',<br>    'email' =&gt; 'ali@example.com'<br>]);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Eloquent Relationships<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">One-to-One Relationship<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public function profile()<br>{<br>    return $this-&gt;hasOne(Profile::class);<br>}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">One-to-Many Relationship<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public function posts()<br>{<br>    return $this-&gt;hasMany(Post::class);<br>}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Many-to-Many Relationship<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>public function roles()<br>{<br>    return $this-&gt;belongsToMany(Role::class);<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using Migrations with Eloquent<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Migration command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan make:migration create_users_table<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Example Migration:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Schema::create('users', function ($table) {<br>    $table-&gt;id();<br>    $table-&gt;string('name');<br>    $table-&gt;string('email');<br>    $table-&gt;timestamps();<br>});<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Run migration:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan migrate<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Eloquent Query Builder<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Ordering Data<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$users = User::orderBy('name', 'ASC')-&gt;get();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Limiting Results<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$users = User::take(5)-&gt;get();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Counting Records<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$count = User::count();<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of Eloquent ORM<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Easy to learn and use<\/li>\n\n\n\n<li>Reduces SQL complexity<\/li>\n\n\n\n<li>Clean and readable code<\/li>\n\n\n\n<li>Faster application development<\/li>\n\n\n\n<li>Secure database interaction<\/li>\n\n\n\n<li>Supports powerful relationships<\/li>\n\n\n\n<li>Integrated with Laravel framework<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use meaningful model names<\/li>\n\n\n\n<li>Protect fields using fillable property<\/li>\n\n\n\n<li>Use migrations for database management<\/li>\n\n\n\n<li>Optimize queries to improve performance<\/li>\n\n\n\n<li>Use relationships instead of manual joins where possible<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Real World Applications<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Eloquent ORM is commonly used in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>E-commerce applications<\/li>\n\n\n\n<li>Content management systems<\/li>\n\n\n\n<li>Learning management systems<\/li>\n\n\n\n<li>CRM software<\/li>\n\n\n\n<li>Inventory systems<\/li>\n\n\n\n<li>Social media platforms<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Career Opportunities<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Learning Eloquent ORM can help you become:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Laravel Developer<\/li>\n\n\n\n<li>Backend Developer<\/li>\n\n\n\n<li>Full Stack Web Developer<\/li>\n\n\n\n<li>PHP Application Developer<\/li>\n\n\n\n<li>API Developer<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Final Presentation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In your final presentation, explain:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>What Eloquent ORM is<\/li>\n\n\n\n<li>Benefits of ORM in web development<\/li>\n\n\n\n<li>How Eloquent interacts with databases<\/li>\n\n\n\n<li>CRUD operations using Eloquent<\/li>\n\n\n\n<li>Relationships in Eloquent ORM<\/li>\n\n\n\n<li>Real-world usage of Eloquent in Laravel applications<\/li>\n<\/ul>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/php\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Professional PHP > Laravel Framework Basics > Eloquent ORM<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1779292560452\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":58,"template":"","class_list":["post-167","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Eloquent ORM - Learn PHP with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn Eloquent ORM in Laravel with CRUD operations, models, migrations, and database relationships for web development.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/php\/?lesson=eloquent-orm\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Eloquent ORM - Learn PHP with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn Eloquent ORM in Laravel with CRUD operations, models, migrations, and database relationships for web development.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/php\/?lesson=eloquent-orm\" \/>\n<meta property=\"og:site_name\" content=\"Learn PHP with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-21T14:43:01+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=eloquent-orm\",\"url\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=eloquent-orm\",\"name\":\"Eloquent ORM - Learn PHP with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/#website\"},\"datePublished\":\"2026-05-20T15:55:53+00:00\",\"dateModified\":\"2026-05-21T14:43:01+00:00\",\"description\":\"Learn Eloquent ORM in Laravel with CRUD operations, models, migrations, and database relationships for web development.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=eloquent-orm#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=eloquent-orm\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=eloquent-orm#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/php\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Professional PHP > Laravel Framework Basics > Eloquent ORM\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/php\\\/\",\"name\":\"Learn PHP with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Eloquent ORM - Learn PHP with GiGz.PK","description":"Learn Eloquent ORM in Laravel with CRUD operations, models, migrations, and database relationships for web development.","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:\/\/gigz.pk\/php\/?lesson=eloquent-orm","og_locale":"en_US","og_type":"article","og_title":"Eloquent ORM - Learn PHP with GiGz.PK","og_description":"Learn Eloquent ORM in Laravel with CRUD operations, models, migrations, and database relationships for web development.","og_url":"https:\/\/gigz.pk\/php\/?lesson=eloquent-orm","og_site_name":"Learn PHP with GiGz.PK","article_modified_time":"2026-05-21T14:43:01+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/php\/?lesson=eloquent-orm","url":"https:\/\/gigz.pk\/php\/?lesson=eloquent-orm","name":"Eloquent ORM - Learn PHP with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/php\/#website"},"datePublished":"2026-05-20T15:55:53+00:00","dateModified":"2026-05-21T14:43:01+00:00","description":"Learn Eloquent ORM in Laravel with CRUD operations, models, migrations, and database relationships for web development.","breadcrumb":{"@id":"https:\/\/gigz.pk\/php\/?lesson=eloquent-orm#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/php\/?lesson=eloquent-orm"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/php\/?lesson=eloquent-orm#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/php"},{"@type":"ListItem","position":2,"name":"Professional PHP > Laravel Framework Basics > Eloquent ORM"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/php\/#website","url":"https:\/\/gigz.pk\/php\/","name":"Learn PHP with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/php\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/lesson\/167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}