{"id":605,"date":"2024-09-20T12:31:26","date_gmt":"2024-09-20T12:31:26","guid":{"rendered":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/?p=605"},"modified":"2024-09-20T12:44:17","modified_gmt":"2024-09-20T12:44:17","slug":"laravel-migrations","status":"publish","type":"post","link":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/laravel-migrations\/","title":{"rendered":"Basics of Laravel Migrations"},"content":{"rendered":"<p>Basics of Laravel Migrations are a powerful feature that help you manage your database schema over time in a structured and version-controlled manner. They are like version control for your database, allowing you to define your database structure using PHP code instead of directly writing SQL.<\/p>\n<p>For any web application or website, managing the integrity and consistency of a database schema over time can be a challenge, particularly when working within collaborative team environments or across various deployment stages.<\/p>\n<p><a href=\"https:\/\/laravel.com\" target=\"_blank\" rel=\"noopener\">Laravel<\/a> offers a robust feature known as <strong>migrations<\/strong> that simplifies this process. Migrations act as version control for your database, enabling developers to modify, share, and track changes to the application\u2019s database schema in a reliable manner. This comprehensive guide introduces the basics of Laravel migrations, providing essential knowledge and practical skills to effectively implement and manage database migrations.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/laravel-migrations\/#Key_Concepts_Basics_of_Laravel_Migrations\" >Key Concepts Basics of Laravel Migrations<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/laravel-migrations\/#Summary_of_Key_Migration_Commands\" >Summary of Key Migration Commands<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/laravel-migrations\/#Best_Practices_for_Managing_Laravel_Migrations\" >Best Practices for Managing Laravel Migrations<\/a><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"Key_Concepts_Basics_of_Laravel_Migrations\"><\/span>Key Concepts Basics of Laravel Migrations<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ol>\n<li><strong>Migration Files<\/strong>:\n<ul>\n<li>Migrations are stored as individual files in the <code>database\/migrations<\/code> directory.<\/li>\n<li>Each migration file is responsible for making changes to the database schema, like creating, modifying, or dropping tables and columns.<\/li>\n<li>Migration file names are timestamped for order and versioning, ensuring that migrations are run in the correct sequence.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Creating a Migration<\/strong>: Use the Artisan command to create a new migration:<\/li>\n<\/ol>\n<pre>php artisan make:migration create_users_table<\/pre>\n<p>This will generate a migration file in the <code>database\/migrations<\/code> folder with a timestamp in its filename, like:<\/p>\n<pre>2024_09_19_145823_create_users_table.php<\/pre>\n<p><strong>\u00a0 \u00a0 3.Migration Structure<\/strong>: A migration file contains two methods:<\/p>\n<ul>\n<li><code>up()<\/code>: Defines the changes to apply to the database, like creating a table or adding columns.<\/li>\n<li><code>down()<\/code>: Defines how to reverse the changes made by <code>up()<\/code>. This is used to roll back migrations.<\/li>\n<\/ul>\n<p>Example of a migration for creating a <code>users<\/code> table:<\/p>\n<pre>&lt;?php\r\n\r\nuse Illuminate\\Database\\Migrations\\Migration;\r\nuse Illuminate\\Database\\Schema\\Blueprint;\r\nuse Illuminate\\Support\\Facades\\Schema;\r\n\r\nclass CreateUsersTable extends Migration\r\n{\r\n\/**\r\n* Run the migrations.\r\n*\r\n* @return void\r\n*\/\r\npublic function up()\r\n{\r\nSchema::create('users', function (Blueprint $table) {\r\n$table-&gt;id();\r\n$table-&gt;string('name');\r\n$table-&gt;string('email')-&gt;unique();\r\n$table-&gt;timestamp('email_verified_at')-&gt;nullable();\r\n$table-&gt;string('password');\r\n$table-&gt;timestamps();\r\n});\r\n}\r\n\r\n\/**\r\n* Reverse the migrations.\r\n*\r\n* @return void\r\n*\/\r\npublic function down()\r\n{\r\nSchema::dropIfExists('users');\r\n}\r\n}<\/pre>\n<p><strong>\u00a0 \u00a04.Running Migrations<\/strong>: After creating your migration files, you can run them using the following command:<\/p>\n<pre>php artisan migrate<\/pre>\n<p>This command will execute all the migrations that haven&#8217;t been run yet.<\/p>\n<p><strong>\u00a0 5.Rolling Back Migrations<\/strong>: You can roll back the last batch of migrations using:<\/p>\n<pre>php artisan migrate:rollback<\/pre>\n<p>This will run the <code>down()<\/code> method of the most recently run migrations.<\/p>\n<p>If you want to reset all migrations, use:<\/p>\n<pre>php artisan migrate:reset<\/pre>\n<p>To refresh the entire database (rolling back and running all migrations again), use:<\/p>\n<pre>php artisan migrate:refresh<\/pre>\n<p><strong>\u00a0 6.Modifying Tables<\/strong>: You can create migrations to modify existing tables. For example, if you want to add a column to an existing table, run:<\/p>\n<pre>php artisan make:migration add_age_to_users_table --table=users<\/pre>\n<p>Then, inside the <code>up()<\/code> method, you would add the column:<\/p>\n<pre>Schema::table('users', function (Blueprint $table) {\r\n$table-&gt;integer('age')-&gt;nullable();\r\n});<\/pre>\n<p>&nbsp;<\/p>\n<p>The <code>down()<\/code> method should remove the column if you roll back the migration:<\/p>\n<pre>Schema::table('users', function (Blueprint $table) {\r\n$table-&gt;dropColumn('age');\r\n});<\/pre>\n<p><strong>\u00a0 7.Seeding Data<\/strong>: Laravel also supports database seeding, which allows you to insert test data into your tables. You can create a seeder with:<\/p>\n<pre>php artisan make:seeder UsersTableSeeder<\/pre>\n<p>To run seeders:<\/p>\n<pre>php artisan db:seed<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Summary_of_Key_Migration_Commands\"><\/span>Summary of Key Migration Commands<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li><strong>Create a migration<\/strong>:<\/li>\n<\/ul>\n<pre>php artisan make:migration migration_name<\/pre>\n<ul>\n<li><strong>Run migrations<\/strong>:<\/li>\n<\/ul>\n<pre>php artisan migrate<\/pre>\n<ul>\n<li><strong>Roll back migrations<\/strong>:<\/li>\n<\/ul>\n<pre>php artisan migrate:rollback<\/pre>\n<ul>\n<li><strong>Reset all migrations<\/strong>:<\/li>\n<\/ul>\n<pre>php artisan migrate:reset<\/pre>\n<ul>\n<li><strong>Refresh migrations<\/strong>:<\/li>\n<\/ul>\n<pre>php artisan migrate:refresh<\/pre>\n<ul>\n<li><strong>Run seeders<\/strong>:<\/li>\n<\/ul>\n<pre>php artisan db:seed<\/pre>\n<h2 id=\"best-practices\" class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Best_Practices_for_Managing_Laravel_Migrations\"><\/span>Best Practices for Managing Laravel Migrations<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To ensure that your migrations are easy to manage and understand, consider following these best practices:<\/p>\n<ul class=\"wp-block-list\">\n<li><strong>Keep Migrations Specific:<\/strong> Limit each migration file to handling a single specific change. This makes migrations easier to understand and manage.<\/li>\n<li><strong>Use Descriptive Names:<\/strong> Choose names for your migration files that clearly describe what the migration does. This helps other developers understand the purpose of each migration without needing to read the entire file.<\/li>\n<li><strong>Commit Regularly:<\/strong> Regularly commit your migration files to your version control system. This keeps your team in sync and allows new team members to set up their development environments quickly by running existing migrations.<\/li>\n<\/ul>\n<p>To look for SSH Essentials Working with SSH Servers, Clients, and Keys <a href=\"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/working-with-ssh-servers\/\">click here.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Basics of Laravel Migrations are a powerful feature that help you manage your database schema over time in a structured and version-controlled manner. They are like version control for your database, allowing you to define your database structure using PHP code instead of directly writing SQL. For any web application or website, managing the integrity [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42,66],"tags":[],"class_list":["post-605","post","type-post","status-publish","format-standard","hentry","category-linux","category-ssh"],"_links":{"self":[{"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/posts\/605","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/comments?post=605"}],"version-history":[{"count":8,"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/posts\/605\/revisions"}],"predecessor-version":[{"id":613,"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/posts\/605\/revisions\/613"}],"wp:attachment":[{"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/media?parent=605"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/categories?post=605"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hyderabadwebhosting.co.in\/tutorials\/wp-json\/wp\/v2\/tags?post=605"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}