{"id":831,"date":"2019-07-11T10:31:44","date_gmt":"2019-07-11T10:31:44","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=831"},"modified":"2019-07-11T10:41:38","modified_gmt":"2019-07-11T10:41:38","slug":"laravel-migration","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/laravel-migration\/","title":{"rendered":"Laravel Migration"},"content":{"rendered":"<p>Migrations are generally used to modify the database schema. Along with Laravel&#8217;s schema builder, it builds the application&#8217;s database schema. Designing tables not only become easy with the use of migrations, but the entire table can also be created.<\/p>\n<p>Migrations are stored in : database\/migrations<\/p>\n<ol>\n<li><strong>Generate Migration:<\/strong><br \/>\nMake:migration &#8211; This command is used to generate a migration<\/p>\n<pre>php artisan make:migration sample_table<\/pre>\n<p>This will generate a migration with the name SampleTable<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2019\/07\/laravel-migration-1.png\" \/><\/li>\n<li><strong>Migration Structure<\/strong><br \/>\nIt contains two methods: up and down.<br \/>\nNew columns, tables or indexes are added to the <a href=\"https:\/\/www.studysection.com\/mongodb-3.x-foundation\" style=\"color:blue\">database<\/a> using the up method.<br \/>\nThe operations performed by the up method are reversed by the down method.<\/p>\n<pre>\r\n&lt;?php\r\n\r\nuse Illuminate\\Support\\Facades\\Schema;\r\nuse Illuminate\\Database\\Schema\\Blueprint;\r\nuse Illuminate\\Database\\Migrations\\Migration;\r\n\r\nclass SampleTable extends Migration\r\n{\r\n    \/**\r\n     * Run the migrations.\r\n     *\r\n     * @return void\r\n     *\/\r\n    public function up()\r\n    {\r\n        \/\/\r\n    }\r\n\r\n    \/**\r\n     * Reverse the migrations.\r\n     *\r\n     * @return void\r\n     *\/\r\n    public function down()\r\n    {\r\n        \/\/\r\n    }\r\n}\r\n<\/pre>\n<\/li>\n<li><strong>Running Migration<\/strong><br \/>\nphp artisan migrate &#8211; this command is used to run the migration.<br \/>\nThis will create the migration and insert an entry into the migration table.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2019\/07\/laravel-migration-2.png\" \/><\/li>\n<li><strong>RollBack a Migration<\/strong><br \/>\n php artisan migrate:rollback &#8211; This command is used to rollback a migration<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2019\/07\/laravel-migration-3.png\" \/><\/li>\n<li><strong>Drop All Tables &#038; Migrate<\/strong><br \/>\nphp artisan migrate:fresh &#8211; This command is used to drop all the tables in the database.<\/li>\n<\/ol>\n<p><strong>Tables<\/strong><\/p>\n<ol>\n<li><strong>Create Table<\/strong><br \/>\nCreate method is used to create a new database table on the Schema facade. It accepts two arguments.<\/p>\n<ul>\n<li>The name of the table<\/li>\n<li>Blueprint object &#8211; Used to define the new table:\n<p>Schema::create(&#8216;sample_table&#8217;, function (Blueprint $table) {<br \/>\n    $table->increments(&#8216;id&#8217;);<br \/>\n});<\/li>\n<\/ul>\n<\/li>\n<li><strong>Rename a Table<\/strong><br \/>\nTo rename an existing method use rename method<\/p>\n<p>Schema::rename($from, $to);<\/p>\n<\/li>\n<li><strong>Drop a Table<\/strong><br \/>\nTo drop the table use drop or dropIfExists method.<\/p>\n<pre>\t\r\n    Schema::drop('sample_table');\r\n    or\r\n    Schema::dropIfExists('sample_table');\r\n<\/pre>\n<\/li>\n<\/ol>\n<p><strong>Run a specific migration<\/strong><br \/>\nWe will create sample_table with some columns:<\/p>\n<pre>\r\n&lt;?php\r\n\r\nuse Illuminate\\Support\\Facades\\Schema;\r\nuse Illuminate\\Database\\Schema\\Blueprint;\r\nuse Illuminate\\Database\\Migrations\\Migration;\r\n\r\nclass SampleTable extends Migration\r\n{\r\n    \/**\r\n     * Run the migrations.\r\n     *\r\n     * @return void\r\n     *\/\r\n    public function up()\r\n    {\r\n          Schema::create('sample_table', function (Blueprint $table) {\r\n            $table->bigIncrements('id');\r\n            $table->string('name');\r\n        });\r\n    }\r\n\r\n    \/**\r\n     * Reverse the migrations.\r\n     *\r\n     * @return void\r\n     *\/\r\n    public function down()\r\n    {\r\n         Schema::dropIfExists('sample_table');\r\n    }\r\n}\r\n<\/pre>\n<p>To run only this migration we need to add the path of this migration in migrate command<\/p>\n<p>php artisan migrate &#8211;path=\/database\/migrations\/2019_04_08_121257_sample_table.php<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2019\/07\/laravel-migration-4.png\" \/><br \/>\nWith this, we can run a specific migration.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2019\/07\/laravel-migration-5.png\" \/><\/p>\n<p>The table is created along with the fields.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Migrations are generally used to modify the database schema. Along with Laravel&#8217;s schema builder, it builds the application&#8217;s database schema.<\/p>\n","protected":false},"author":1,"featured_media":849,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[149,65,148,150,151],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>StudySection Blog - Laravel Migration to Modify Database<\/title>\n<meta name=\"description\" content=\"Migrations are generally used to modify the database schema. Along with Laravel\u2019s schema builder, it builds the application\u2019s database schema.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/laravel-migration\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"StudySection Blog - Laravel Migration to Modify Database\" \/>\n<meta property=\"og:description\" content=\"Migrations are generally used to modify the database schema. Along with Laravel\u2019s schema builder, it builds the application\u2019s database schema.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/laravel-migration\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2019-07-11T10:31:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-07-11T10:41:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2019\/07\/laravel-blog.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"160\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/laravel-migration\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/laravel-migration\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Laravel Migration\",\"datePublished\":\"2019-07-11T10:31:44+00:00\",\"dateModified\":\"2019-07-11T10:41:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/laravel-migration\/\"},\"wordCount\":295,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"database\",\"laravel\",\"migration\",\"structure\",\"tables\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/laravel-migration\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/laravel-migration\/\",\"url\":\"https:\/\/studysection.com\/blog\/laravel-migration\/\",\"name\":\"StudySection Blog - Laravel Migration to Modify Database\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2019-07-11T10:31:44+00:00\",\"dateModified\":\"2019-07-11T10:41:38+00:00\",\"description\":\"Migrations are generally used to modify the database schema. Along with Laravel\u2019s schema builder, it builds the application\u2019s database schema.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/laravel-migration\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/laravel-migration\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/laravel-migration\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Laravel Migration\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"StudySection Blog - Laravel Migration to Modify Database","description":"Migrations are generally used to modify the database schema. Along with Laravel\u2019s schema builder, it builds the application\u2019s database schema.","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:\/\/studysection.com\/blog\/laravel-migration\/","og_locale":"en_US","og_type":"article","og_title":"StudySection Blog - Laravel Migration to Modify Database","og_description":"Migrations are generally used to modify the database schema. Along with Laravel\u2019s schema builder, it builds the application\u2019s database schema.","og_url":"https:\/\/studysection.com\/blog\/laravel-migration\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2019-07-11T10:31:44+00:00","article_modified_time":"2019-07-11T10:41:38+00:00","og_image":[{"width":300,"height":160,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2019\/07\/laravel-blog.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/laravel-migration\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/laravel-migration\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Laravel Migration","datePublished":"2019-07-11T10:31:44+00:00","dateModified":"2019-07-11T10:41:38+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/laravel-migration\/"},"wordCount":295,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["database","laravel","migration","structure","tables"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/laravel-migration\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/laravel-migration\/","url":"https:\/\/studysection.com\/blog\/laravel-migration\/","name":"StudySection Blog - Laravel Migration to Modify Database","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2019-07-11T10:31:44+00:00","dateModified":"2019-07-11T10:41:38+00:00","description":"Migrations are generally used to modify the database schema. Along with Laravel\u2019s schema builder, it builds the application\u2019s database schema.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/laravel-migration\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/laravel-migration\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/laravel-migration\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Laravel Migration"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":664,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/831"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=831"}],"version-history":[{"count":16,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/831\/revisions"}],"predecessor-version":[{"id":853,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/831\/revisions\/853"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/849"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}