{"id":3919,"date":"2024-01-17T12:30:53","date_gmt":"2024-01-17T12:30:53","guid":{"rendered":"https:\/\/www.itwebsols.com\/?p=3919"},"modified":"2024-01-17T12:30:53","modified_gmt":"2024-01-17T12:30:53","slug":"database-management-with-eloquent-orm-in-laravel","status":"publish","type":"post","link":"https:\/\/v5.itwebsols.com\/index.php\/2024\/01\/17\/database-management-with-eloquent-orm-in-laravel\/","title":{"rendered":"Database Management with Eloquent ORM in Laravel"},"content":{"rendered":"<p>Laravel utilizes Eloquent ORM, a powerful and expressive ORM that simplifies database management and interaction with databases. Here&#8217;s a guide on using Eloquent ORM for database management in Laravel:<\/p>\n<ol>\n<li><strong>Configuration:<\/strong>\n<ul>\n<li>Configure database settings in the <code>.env<\/code> file, specifying the database connection details such as DB_CONNECTION, DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME, and DB_PASSWORD.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Defining Models:<\/strong>\n<ul>\n<li>Create a model for each database table. Models represent tables and are used to interact with the database.<\/li>\n<\/ul>\n<div class=\"bg-black rounded-md\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 gizmo:dark:bg-token-surface-primary px-4 py-2 text-xs font-sans justify-between rounded-t-md\">php<\/div>\n<div class=\"p-4 overflow-y-auto\">\n<p><code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span><\/code><\/p>\n<p><span class=\"hljs-keyword\">namespace<\/span> <span class=\"hljs-title class_\">App<\/span>;<\/p>\n<p><span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">Illuminate<\/span>\\<span class=\"hljs-title\">Database<\/span>\\<span class=\"hljs-title\">Eloquent<\/span>\\<span class=\"hljs-title\">Model<\/span>;<\/p>\n<p><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">YourModel<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">Model<\/span><br \/>\n<\/span>{<br \/>\n<span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-variable\">$table<\/span> = <span class=\"hljs-string\">&#8216;your_table_name&#8217;<\/span>; <span class=\"hljs-comment\">\/\/ Specify table name if it differs from model name<\/span><br \/>\n<span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-variable\">$primaryKey<\/span> = <span class=\"hljs-string\">&#8216;id&#8217;<\/span>; <span class=\"hljs-comment\">\/\/ Specify primary key if it differs from &#8216;id&#8217;<\/span><br \/>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-variable\">$timestamps<\/span> = <span class=\"hljs-literal\">true<\/span>; <span class=\"hljs-comment\">\/\/ Set to false if timestamps are not used<\/span><\/p>\n<p><span class=\"hljs-comment\">\/\/ Define relationships, accessors, mutators, and other model-specific logic here<\/span><br \/>\n}<\/p>\n<\/div>\n<\/div>\n<\/li>\n<li><strong>CRUD Operations (Create, Read, Update, Delete):<\/strong>\n<ul>\n<li>Eloquent simplifies CRUD operations. Use methods provided by Eloquent to interact with the database.<\/li>\n<\/ul>\n<div class=\"bg-black rounded-md\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 gizmo:dark:bg-token-surface-primary px-4 py-2 text-xs font-sans justify-between rounded-t-md\">php<\/div>\n<div class=\"p-4 overflow-y-auto\">\n<p><code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">App<\/span>\\<span class=\"hljs-title\">YourModel<\/span>;<\/code><\/p>\n<p><span class=\"hljs-comment\">\/\/ Create<\/span><br \/>\n<span class=\"hljs-variable\">$newEntity<\/span> = <span class=\"hljs-title class_\">YourModel<\/span>::<span class=\"hljs-title function_ invoke__\">create<\/span>([<br \/>\n<span class=\"hljs-string\">&#8216;column1&#8217;<\/span> =&gt; <span class=\"hljs-string\">&#8216;value1&#8217;<\/span>,<br \/>\n<span class=\"hljs-string\">&#8216;column2&#8217;<\/span> =&gt; <span class=\"hljs-string\">&#8216;value2&#8217;<\/span>,<br \/>\n\/\/ other columns<br \/>\n]);<\/p>\n<p><span class=\"hljs-comment\">\/\/ Read<\/span><br \/>\n<span class=\"hljs-variable\">$entity<\/span> = <span class=\"hljs-title class_\">YourModel<\/span>::<span class=\"hljs-title function_ invoke__\">find<\/span>(<span class=\"hljs-variable\">$id<\/span>); <span class=\"hljs-comment\">\/\/ Find by primary key<\/span><\/p>\n<p><span class=\"hljs-comment\">\/\/ Update<\/span><br \/>\n<span class=\"hljs-variable\">$entity<\/span>-&gt;column1 = <span class=\"hljs-string\">&#8216;new value&#8217;<\/span>;<br \/>\n<span class=\"hljs-variable\">$entity<\/span>-&gt;<span class=\"hljs-title function_ invoke__\">save<\/span>();<\/p>\n<p><span class=\"hljs-comment\">\/\/ Delete<\/span><br \/>\n<span class=\"hljs-variable\">$entity<\/span>-&gt;<span class=\"hljs-title function_ invoke__\">delete<\/span>();<\/p>\n<\/div>\n<\/div>\n<\/li>\n<li><strong>Relationships:<\/strong>\n<ul>\n<li>Define relationships between models using Eloquent&#8217;s methods (hasOne, hasMany, belongsTo, etc.) to represent associations between tables.<\/li>\n<\/ul>\n<div class=\"bg-black rounded-md\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 gizmo:dark:bg-token-surface-primary px-4 py-2 text-xs font-sans justify-between rounded-t-md\">php<\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-php\"><code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">User<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">Model<\/span><br \/>\n<\/span>{<br \/>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">posts<\/span>()<br \/>\n<\/span>{<br \/>\n<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-variable language_\">$this<\/span>-&gt;<span class=\"hljs-title function_ invoke__\">hasMany<\/span>(<span class=\"hljs-string\">'App\\Post'<\/span>);<br \/>\n}<br \/>\n}<\/code><\/code><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Post<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">Model<\/span><br \/>\n<\/span>{<br \/>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">user<\/span>()<br \/>\n<\/span>{<br \/>\n<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-variable language_\">$this<\/span>-&gt;<span class=\"hljs-title function_ invoke__\">belongsTo<\/span>(<span class=\"hljs-string\">&#8216;App\\User&#8217;<\/span>);<br \/>\n}<br \/>\n}<\/div>\n<\/div>\n<\/li>\n<li><strong>Migrations:<\/strong>\n<ul>\n<li>Use migrations to create and manage database tables. Migrations define the structure of the database tables using PHP code.<\/li>\n<\/ul>\n<div class=\"bg-black rounded-md\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 gizmo:dark:bg-token-surface-primary px-4 py-2 text-xs font-sans justify-between rounded-t-md\">bash<\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-bash\">php artisan make:migration create_table_name<br \/>\n<\/code><\/div>\n<\/div>\n<\/li>\n<li><strong>Database Seeding:<\/strong>\n<ul>\n<li>Seed the database with dummy data for testing or development purposes using seeders and factories.<\/li>\n<\/ul>\n<div class=\"bg-black rounded-md\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 gizmo:dark:bg-token-surface-primary px-4 py-2 text-xs font-sans justify-between rounded-t-md\">bash<\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-bash\">php artisan make:seeder YourSeederName<br \/>\nphp artisan db:seed --class=YourSeederName<br \/>\n<\/code><\/div>\n<\/div>\n<\/li>\n<li><strong>Query Scopes:<\/strong>\n<ul>\n<li>Use query scopes to define reusable query constraints within your models.<\/li>\n<\/ul>\n<div class=\"bg-black rounded-md\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 gizmo:dark:bg-token-surface-primary px-4 py-2 text-xs font-sans justify-between rounded-t-md\">php<\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-php\"><code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">YourModel<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">Model<\/span><br \/>\n<\/span>{<br \/>\n<span class=\"hljs-comment\">\/\/ Scope for filtering active records<\/span><br \/>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">scopeActive<\/span>(<span class=\"hljs-params\"><span class=\"hljs-variable\">$query<\/span><\/span>)<br \/>\n<\/span>{<br \/>\n<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-variable\">$query<\/span>-&gt;<span class=\"hljs-title function_ invoke__\">where<\/span>(<span class=\"hljs-string\">'status'<\/span>, <span class=\"hljs-string\">'active'<\/span>);<br \/>\n}<br \/>\n}<\/code><\/code><span class=\"hljs-comment\">\/\/ Usage<\/span><br \/>\n<span class=\"hljs-variable\">$activeEntities<\/span> = <span class=\"hljs-title class_\">YourModel<\/span>::<span class=\"hljs-title function_ invoke__\">active<\/span>()-&gt;<span class=\"hljs-title function_ invoke__\">get<\/span>();<\/div>\n<\/div>\n<\/li>\n<li><strong>Eager Loading:<\/strong>\n<ul>\n<li>Utilize eager loading to load relationships to avoid N+1 query issues and improve performance.<\/li>\n<\/ul>\n<div class=\"bg-black rounded-md\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 gizmo:dark:bg-token-surface-primary px-4 py-2 text-xs font-sans justify-between rounded-t-md\">php<\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-variable\">$posts<\/span> = <span class=\"hljs-title class_\">Post<\/span>::<span class=\"hljs-title function_ invoke__\">with<\/span>(<span class=\"hljs-string\">'user'<\/span>)-&gt;<span class=\"hljs-title function_ invoke__\">get<\/span>(); <span class=\"hljs-comment\">\/\/ Eager load 'user' relationship<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<\/li>\n<li><strong>Validation:<\/strong>\n<ul>\n<li>Use Laravel&#8217;s validation features to validate input data before performing database operations.<\/li>\n<\/ul>\n<div class=\"bg-black rounded-md\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 gizmo:dark:bg-token-surface-primary px-4 py-2 text-xs font-sans justify-between rounded-t-md\">php<\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-php\"><span class=\"hljs-variable\">$validatedData<\/span> = <span class=\"hljs-variable\">$request<\/span>-&gt;<span class=\"hljs-title function_ invoke__\">validate<\/span>([<br \/>\n<span class=\"hljs-string\">'column1'<\/span> =&gt; <span class=\"hljs-string\">'required|max:255'<\/span>,<br \/>\n<span class=\"hljs-string\">'column2'<\/span> =&gt; <span class=\"hljs-string\">'required|numeric'<\/span>,<br \/>\n\/\/ other validation rules<br \/>\n]);<br \/>\n<\/code><\/div>\n<\/div>\n<\/li>\n<\/ol>\n<p>Eloquent ORM in Laravel simplifies database operations, making it easier to work with databases in your Laravel applications. Always follow Laravel conventions and best practices while utilizing Eloquent ORM for efficient database management and manipulation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Laravel utilizes Eloquent ORM, a powerful and expressive ORM that simplifies database management and interaction with databases. Here&#8217;s a guide on using Eloquent ORM for database management in Laravel: Configuration: Configure database settings in the .env file, specifying the database connection details such as DB_CONNECTION, DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME, and DB_PASSWORD. Defining Models: Create a&#8230;<\/p>\n","protected":false},"author":1,"featured_media":3967,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-3919","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","th-blog blog-single has-post-thumbnail"],"_links":{"self":[{"href":"https:\/\/v5.itwebsols.com\/index.php\/wp-json\/wp\/v2\/posts\/3919","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/v5.itwebsols.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/v5.itwebsols.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/v5.itwebsols.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/v5.itwebsols.com\/index.php\/wp-json\/wp\/v2\/comments?post=3919"}],"version-history":[{"count":0,"href":"https:\/\/v5.itwebsols.com\/index.php\/wp-json\/wp\/v2\/posts\/3919\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/v5.itwebsols.com\/index.php\/wp-json\/wp\/v2\/media\/3967"}],"wp:attachment":[{"href":"https:\/\/v5.itwebsols.com\/index.php\/wp-json\/wp\/v2\/media?parent=3919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/v5.itwebsols.com\/index.php\/wp-json\/wp\/v2\/categories?post=3919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/v5.itwebsols.com\/index.php\/wp-json\/wp\/v2\/tags?post=3919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}