{"flag":true,"single":true,"pageTitle":"How to Use Laravel Migrations for Efficient Database Management","post":{"id":112,"user_id":"1","slug":"how-to-use-laravel-migrations-for-efficient-database-management-zxgj","title":"How to Use Laravel Migrations for Efficient Database Management","body":"<p><strong>Migrations<\/strong> (structure of table ), create tables in phpmyadmin auto using structure<\/p>\r\n<p>Laravel migrations are a database version control system built into the Laravel PHP framework. They allow you to define and modify the <strong>structure of your database<\/strong> tables using PHP code, and track changes to the schema over time.<\/p>\r\n<p>With Laravel migrations, you can create new tables, modify existing tables, and even add new columns or indexes to tables that are already in use.<\/p>\r\n<p>When you run a migration, Laravel will apply the changes defined in the migration file to your database. You can also roll back migrations, which will undo the changes made by a particular migration. This makes it easy to test and modify your database schema over time, without worrying about losing data or having to manually update your database structure.<\/p>\r\n<p>every table should has its own model<\/p>\r\n<p><strong>create migration with model,&nbsp;<\/strong>use singular model name laravel make it auto plural<\/p>\r\n<pre class=\"language-markup\"><code>php artisan make:model Customer -m<\/code><\/pre>\r\n<p>it will create an model inside <strong>http&gt;models<\/strong> and migration inside <strong>database&gt;migrations<\/strong><\/p>\r\n<p>Now edit customer migration as&nbsp;<\/p>\r\n<pre class=\"language-markup\"><code> public function up()\r\n    {\r\n        Schema::create('customers', function (Blueprint $table) {\r\n            \t\t$table-&gt;id(); \/\/primary key\r\n\t\t\t$table-&gt;string('name');\r\n\t\t\t$table-&gt;string('email')-&gt;unique();\r\n\t\t\t$table-&gt;string('address')-&gt;nullable(); \/\/optional\r\n\t\t\t$table-&gt;string('phone');\r\n            \t\t$table-&gt;timestamps(); \/\/created_at and updated_at column\r\n        });\r\n    }<\/code><\/pre>\r\n<p>run migtation, it will create all tables that are defined in migrations<\/p>\r\n<pre class=\"language-markup\"><code>php artisan migrate<\/code><\/pre>\r\n<p>To delete last create table<\/p>\r\n<pre class=\"language-markup\"><code>php artisan migrate:rollback --step=1<\/code><\/pre>\r\n<p><strong>To modify migration use below commands<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>php artisan migrate:reset\r\nphp artisan migrate<\/code><\/pre>","category_id":"2","is_private":"0","created_at":"2023-04-13T00:40:08.000000Z","updated_at":"2023-04-13T00:40:08.000000Z","category":{"id":2,"user_id":"1","name":"Laravel Core","slug":"laravel-nhyt","parent_id":"1","created_at":"2023-03-14T03:58:27.000000Z","updated_at":"2023-03-20T11:30:50.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"Migrations (structure of table ), create tables in phpmyadmin auto using structure Laravel migrations are a database version control system  - How to Use Laravel Migrations for Efficient Database Management (Updated: April 13, 2023) - Read more about How to Use Laravel Migrations for Efficient Database Management at my programming site [SITE]","categories":[]}