{"id":75695,"date":"2024-07-21T21:36:32","date_gmt":"2024-07-21T14:36:32","guid":{"rendered":"https:\/\/itviec.com\/blog\/?p=75695"},"modified":"2024-07-21T21:36:32","modified_gmt":"2024-07-21T14:36:32","slug":"laravel-migration-la-gi","status":"publish","type":"post","link":"https:\/\/itviec.com\/blog\/laravel-migration-la-gi\/","title":{"rendered":"Laravel Migration: H\u01b0\u1edbng d\u1eabn \u00e1p d\u1ee5ng migration trong Laravel"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 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\">N\u1ed9i dung b\u00e0i vi\u1ebft<\/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 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/itviec.com\/blog\/laravel-migration-la-gi\/#Laravel_migration_la_gi\" >Laravel migration l\u00e0 g\u00ec?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/itviec.com\/blog\/laravel-migration-la-gi\/#Khi_nao_nen_su_dung_Laravel_migration\" >Khi n\u00e0o n\u00ean s\u1eed d\u1ee5ng Laravel migration?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/itviec.com\/blog\/laravel-migration-la-gi\/#Lam_the_nao_de_tao_Laravel_migration\" >L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 t\u1ea1o Laravel migration?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/itviec.com\/blog\/laravel-migration-la-gi\/#Laravel_migration_doi_voi_Table_bang\" >Laravel migration \u0111\u1ed1i v\u1edbi Table (b\u1ea3ng)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/itviec.com\/blog\/laravel-migration-la-gi\/#Laravel_migration_doi_voi_Column_Cot\" >Laravel migration \u0111\u1ed1i v\u1edbi Column (C\u1ed9t)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/itviec.com\/blog\/laravel-migration-la-gi\/#Laravel_migration_doi_voi_Index\" >Laravel migration \u0111\u1ed1i v\u1edbi Index<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/itviec.com\/blog\/laravel-migration-la-gi\/#Events\" >Events<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/itviec.com\/blog\/laravel-migration-la-gi\/#Cau_hoi_thuong_gap_ve_Laravel_migration\" >C\u00e2u h\u1ecfi th\u01b0\u1eddng g\u1eb7p v\u1ec1 Laravel migration<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/itviec.com\/blog\/laravel-migration-la-gi\/#Tong_ket_Laravel_migration\" >T\u1ed5ng k\u1ebft Laravel migration<\/a><\/li><\/ul><\/nav><\/div>\n<p><em><b>Laravel migration l\u00e0 c\u00f4ng c\u1ee5 qu\u1ea3n l\u00fd c\u01a1 s\u1edf d\u1eef li\u1ec7u hi\u1ec7u qu\u1ea3, t\u1eeb vi\u1ec7c t\u1ea1o b\u1ea3ng \u0111\u1ebfn thay \u0111\u1ed5i c\u1ea5u tr\u00fac. V\u1edbi c\u00e1c c\u00e2u l\u1ec7nh PHP \u0111\u01a1n gi\u1ea3n, Developer c\u00f3 th\u1ec3 t\u1ef1 tin th\u1ef1c hi\u1ec7n thay \u0111\u1ed5i m\u00e0 kh\u00f4ng \u1ea3nh h\u01b0\u1edfng \u0111\u1ebfn d\u1eef li\u1ec7u hi\u1ec7n t\u1ea1i. B\u00e0i vi\u1ebft n\u00e0y s\u1ebd h\u01b0\u1edbng d\u1eabn b\u1ea1n t\u1eebng b\u01b0\u1edbc \u0111\u1ec3 s\u1eed d\u1ee5ng Laravel migration m\u1ed9t c\u00e1ch th\u00e0nh th\u1ea1o, t\u1eeb kh\u1edfi t\u1ea1o v\u00e0 qu\u1ea3n l\u00fd b\u1ea3ng cho \u0111\u1ebfn th\u1ef1c hi\u1ec7n c\u00e1c thay \u0111\u1ed5i c\u1ea5u tr\u00fac ph\u1ee9c t\u1ea1p. H\u00e3y c\u00f9ng kh\u00e1m ph\u00e1 c\u00e1ch t\u1eadn d\u1ee5ng s\u1ee9c m\u1ea1nh c\u1ee7a Migration \u0111\u1ec3 x\u00e2y d\u1ef1ng \u1ee9ng d\u1ee5ng Laravel v\u1eefng ch\u1eafc v\u00e0 d\u1ec5 b\u1ea3o tr\u00ec.<\/b><\/em><\/p>\n<p><span style=\"font-weight: 400;\">\u0110\u1ecdc b\u00e0i vi\u1ebft \u0111\u1ec3 n\u1eafm r\u00f5:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Laravel migration l\u00e0 g\u00ec?<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Khi n\u00e0o n\u00ean s\u1eed d\u1ee5ng Laravel migration?<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 t\u1ea1o v\u00e0 s\u1eed d\u1ee5ng Laravel migration?<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e1c c\u00e2u h\u1ecfi th\u01b0\u1eddng g\u1eb7p v\u1ec1 Laravel migration<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Laravel_migration_la_gi\"><\/span><b>Laravel migration l\u00e0 g\u00ec?<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Laravel migration l\u00e0 m\u1ed9t t\u1eadp h\u1ee3p c\u00e1c h\u01b0\u1edbng d\u1eabn x\u00e1c \u0111\u1ecbnh nh\u1eefng thay \u0111\u1ed5i b\u1ea1n mu\u1ed1n th\u1ef1c hi\u1ec7n \u0111\u1ed1i v\u1edbi l\u01b0\u1ee3c \u0111\u1ed3 c\u01a1 s\u1edf d\u1eef li\u1ec7u (database schema). Nh\u1eefng thay \u0111\u1ed5i n\u00e0y c\u00f3 th\u1ec3 bao g\u1ed3m vi\u1ec7c t\u1ea1o b\u1ea3ng m\u1edbi, thay \u0111\u1ed5i b\u1ea3ng hi\u1ec7n c\u00f3, th\u00eam ho\u1eb7c s\u1eeda \u0111\u1ed5i c\u1ed9t v\u00e0 gieo d\u1eef li\u1ec7u ban \u0111\u1ea7u v\u00e0o database. B\u1eb1ng c\u00e1ch g\u00f3i g\u1ecdn nh\u1eefng thay \u0111\u1ed5i n\u00e0y trong c\u00e1c t\u1ec7p migration, Laravel \u0111\u1ea3m b\u1ea3o r\u1eb1ng database schema c\u1ee7a b\u1ea1n v\u1eabn \u0111\u01b0\u1ee3c \u0111\u1ed3ng b\u1ed9 h\u00f3a v\u1edbi codebase c\u1ee7a \u1ee9ng d\u1ee5ng, gi\u00fap qu\u1ea3n l\u00fd c\u00e1c thay \u0111\u1ed5i database tr\u00ean c\u00e1c m\u00f4i tr\u01b0\u1eddng ph\u00e1t tri\u1ec3n v\u00e0 giai \u0111o\u1ea1n tri\u1ec3n khai d\u1ec5 d\u00e0ng h\u01a1n.<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><a href=\"https:\/\/itviec.com\/blog\/laravel-la-gi\/\" target=\"_blank\" rel=\"noopener\"><strong>Laravel<\/strong><\/a> cho ph\u00e9p b\u1ea1n th\u1ef1c hi\u1ec7n migration m\u00e0 kh\u00f4ng ph\u1ea3i lo l\u1eafng v\u1ec1 h\u1ec7 th\u1ed1ng database c\u1ee5 th\u1ec3 b\u1ea1n \u0111ang s\u1eed d\u1ee5ng, d\u00f9 \u0111\u00f3 l\u00e0 MySQL, PostgreSQL, SQLite hay c\u00e1c h\u1ec7 th\u1ed1ng kh\u00e1c \u0111\u01b0\u1ee3c Laravel h\u1ed7 tr\u1ee3. Framework n\u00e0y tr\u1eebu t\u01b0\u1ee3ng h\u00f3a c\u00fa ph\u00e1p d\u00e0nh ri\u00eang cho database, gi\u00fap vi\u1ec7c di chuy\u1ec3n v\u1eeba di \u0111\u1ed9ng v\u1eeba c\u00f3 th\u1ec3 th\u00edch \u1ee9ng v\u1edbi c\u00e1c h\u1ec7 th\u1ed1ng database kh\u00e1c nhau. Laravel migration tr\u1edf th\u00e0nh c\u00f4ng c\u1ee5 b\u1eaft bu\u1ed9c ph\u1ea3i c\u00f3 \u0111\u1ed1i v\u1edbi Web Developer l\u00e0m vi\u1ec7c tr\u00ean Laravel PHP framework.<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Khi_nao_nen_su_dung_Laravel_migration\"><\/span><b> Khi n\u00e0o n\u00ean s\u1eed d\u1ee5ng Laravel migration?<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">T\u1eadn d\u1ee5ng Laravel migration b\u1ea5t c\u1ee9 khi n\u00e0o c\u1ea7n s\u1eeda \u0111\u1ed5i database schemas c\u1ee7a \u1ee9ng d\u1ee5ng nh\u01b0 t\u1ea1o b\u1ea3ng m\u1edbi, th\u00eam c\u1ed9t, s\u1eeda \u0111\u1ed5i b\u1ea3ng hi\u1ec7n c\u00f3 ho\u1eb7c x\u00f3a to\u00e0n b\u1ed9 b\u1ea3ng. Laravel migration cung c\u1ea5p kh\u1ea3 n\u0103ng ki\u1ec3m so\u00e1t phi\u00ean b\u1ea3n cho schemas v\u00e0 l\u00e0 ph\u01b0\u01a1ng ph\u00e1p an to\u00e0n \u0111\u1ec3 qu\u1ea3n l\u00fd c\u00e1c thay \u0111\u1ed5i database m\u00e0 kh\u00f4ng c\u1ea7n ph\u1ea3i vi\u1ebft tr\u1ef1c ti\u1ebfp c\u00e1c truy v\u1ea5n SQL.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">M\u1ed9t s\u1ed1 l\u00fd do ch\u00ednh khi\u1ebfn Laravel migration \u0111\u01b0\u1ee3c l\u1ef1a ch\u1ecdn trong c\u00e1c d\u1ef1 \u00e1n ph\u00e1t tri\u1ec3n web:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Qu\u1ea3n l\u00fd v\u00e0 ki\u1ec3m so\u00e1t phi\u00ean b\u1ea3n c\u00e1c database schemas, cho ph\u00e9p thay \u0111\u1ed5i c\u1ea5u tr\u00fac database theo th\u1eddi gian.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e1ch ti\u1ebfp c\u1eadn database cho ph\u00e9p vi\u1ebft c\u00e1c thay \u0111\u1ed5i schemas m\u1ed9t l\u1ea7n v\u00e0 d\u1ec5 d\u00e0ng chuy\u1ec3n \u0111\u1ed5i gi\u1eefa c\u00e1c h\u1ec7 th\u1ed1ng database kh\u00e1c nhau, ch\u1eb3ng h\u1ea1n nh\u01b0 MySQL, PostgreSQL, SQLite ho\u1eb7c SQL Server.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Cung c\u1ea5p c\u00f4ng c\u1ee5 kh\u00f4i ph\u1ee5c c\u00e1c thay \u0111\u1ed5i database, cho ph\u00e9p Developer kh\u00f4i ph\u1ee5c sau l\u1ed7i, \u0111i\u1ec1u ch\u1ec9nh schemas v\u00e0 duy tr\u00ec tr\u1ea1ng th\u00e1i database \u1ed5n \u0111\u1ecbnh.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">M\u1ed7i t\u1ec7p migration ch\u1ee9a timestamp v\u00e0 m\u00f4 t\u1ea3 v\u1ec1 nh\u1eefng thay \u0111\u1ed5i \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n, c\u1ea3i thi\u1ec7n t\u00ednh minh b\u1ea1ch v\u00e0 kh\u1ea3 n\u0103ng b\u1ea3o tr\u00ec c\u1ee7a codebase.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Ki\u1ec3m so\u00e1t l\u1ecbch s\u1eed v\u00e0 \u0111\u01b0a ra c\u00e1c gi\u1edbi h\u1ea1n kh\u00f4i ph\u1ee5c.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u00edch h\u1ee3p li\u1ec1n m\u1ea1ch v\u1edbi th\u1eed nghi\u1ec7m cho ph\u00e9p Developer t\u1ea1o database th\u1eed nghi\u1ec7m c\u00f3 c\u00f9ng schemas v\u1edbi database ch\u00ednh c\u1ee7a \u1ee9ng d\u1ee5ng, gi\u00fap th\u1ef1c hi\u1ec7n c\u00e1c th\u1eed nghi\u1ec7m li\u00ean quan \u0111\u1ebfn database d\u1ec5 d\u00e0ng h\u01a1n<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u00ednh nh\u1ea5t qu\u00e1n c\u1ee7a codebase<\/span> <span style=\"font-weight: 400;\">\u0111\u1ea3m b\u1ea3o c\u00e1c Developer l\u00e0m vi\u1ec7c trong d\u1ef1 \u00e1n \u0111\u1ec1u c\u00f3 th\u1ec3 \u00e1p d\u1ee5ng thay \u0111\u1ed5i n\u00e0y m\u1ed9t c\u00e1ch th\u1ed1ng nh\u1ea5t tr\u00ean c\u00e1c m\u00f4i tr\u01b0\u1eddng kh\u00e1c nhau.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u00ednh n\u0103ng qu\u1ea3n l\u00fd ph\u1ee5 thu\u1ed9c gi\u00fap \u0111\u01a1n gi\u1ea3n h\u00f3a qu\u00e1 tr\u00ecnh qu\u1ea3n l\u00fd c\u00e1c thay \u0111\u1ed5i database ph\u1ee9c t\u1ea1p.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">B\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng c\u00e1c h\u1ec7 th\u1ed1ng ki\u1ec3m so\u00e1t phi\u00ean b\u1ea3n nh\u01b0 Git., m\u1ecdi thay \u0111\u1ed5i v\u1ec1 schemas \u0111\u1ec1u \u0111\u01b0\u1ee3c chia s\u1ebb v\u00e0 h\u1ee3p nh\u1ea5t, d\u1ec5 d\u00e0ng \u0111\u01b0\u1ee3c xem x\u00e9t, kh\u00f4i ph\u1ee5c ho\u1eb7c \u00e1p d\u1ee5ng tr\u00ean c\u00e1c m\u00f4i tr\u01b0\u1eddng ph\u00e1t tri\u1ec3n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Laravel migration gi\u00fap t\u1ef1 \u0111\u1ed9ng h\u00f3a c\u00e1c thao t\u00e1c database, ti\u1ebft ki\u1ec7m th\u1eddi gian cho developer v\u00e0 gi\u1ea3m thi\u1ec3u l\u1ed7i.<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Lam_the_nao_de_tao_Laravel_migration\"><\/span><b>L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 t\u1ea1o Laravel migration?<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>T\u1ea1o Laravel migration<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng l\u1ec7nh Artisan <\/span><span style=\"font-weight: 400;\">make:migration<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 t\u1ea1o di chuy\u1ec3n c\u01a1 s\u1edf d\u1eef li\u1ec7u (database migration). C\u00e1c migration m\u1edbi s\u1ebd \u0111\u01b0\u1ee3c \u0111\u1eb7t trong th\u01b0 m\u1ee5c <\/span><span style=\"font-weight: 400;\">database\/migrations<\/span><span style=\"font-weight: 400;\"> c\u1ee7a b\u1ea1n. M\u1ed7i t\u00ean t\u1ec7p migration ch\u1ee9a timestamp cho ph\u00e9p Laravel x\u00e1c \u0111\u1ecbnh th\u1ee9 t\u1ef1 di chuy\u1ec3n:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">php artisan make:migration create_flights_table<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Laravel s\u1eed d\u1ee5ng t\u00ean c\u1ee7a migration \u0111\u1ec3 c\u1ed1 g\u1eafng \u0111o\u00e1n t\u00ean c\u1ee7a b\u1ea3ng v\u00e0 li\u1ec7u qu\u00e1 tr\u00ecnh migration c\u00f3 t\u1ea1o m\u1ed9t b\u1ea3ng m\u1edbi hay kh\u00f4ng. N\u1ebfu Laravel c\u00f3 th\u1ec3 x\u00e1c \u0111\u1ecbnh t\u00ean b\u1ea3ng t\u1eeb t\u00ean migration, Laravel s\u1ebd \u0111i\u1ec1n tr\u01b0\u1edbc v\u00e0o t\u1ec7p migration \u0111\u01b0\u1ee3c t\u1ea1o b\u1eb1ng b\u1ea3ng \u0111\u00e3 ch\u1ec9 \u0111\u1ecbnh. N\u1ebfu kh\u00f4ng, b\u1ea1n c\u1ea7n ch\u1ec9 \u0111\u1ecbnh b\u1ea3ng trong t\u1ec7p migration theo c\u00e1ch th\u1ee7 c\u00f4ng.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">N\u1ebfu mu\u1ed1n ch\u1ec9 \u0111\u1ecbnh \u0111\u01b0\u1eddng d\u1eabn t\u00f9y ch\u1ec9nh cho migration \u0111\u01b0\u1ee3c t\u1ea1o, b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng t\u00f9y ch\u1ec9nh <\/span><span style=\"font-weight: 400;\">&#8211;path<\/span><span style=\"font-weight: 400;\"> khi th\u1ef1c hi\u1ec7n l\u1ec7nh <\/span><span style=\"font-weight: 400;\">make:migration<\/span><span style=\"font-weight: 400;\">. \u0110\u01b0\u1eddng d\u1eabn \u0111\u01b0\u1ee3c cung c\u1ea5p ph\u1ea3i t\u01b0\u01a1ng \u0111\u1ed1i v\u1edbi base path c\u1ee7a \u1ee9ng d\u1ee5ng.<\/span><\/p>\n<h3><b>Kh\u1edfi ch\u1ea1y Laravel migration<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 ch\u1ea1y t\u1ea5t c\u1ea3 c\u00e1c b\u1ea3n migration \u0111ang ch\u1edd x\u1eed l\u00fd, h\u00e3y th\u1ef1c hi\u1ec7n l\u1ec7nh Artisan <\/span><span style=\"font-weight: 400;\">migrate<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">php artisan migrate<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">N\u1ebfu b\u1ea1n mu\u1ed1n xem nh\u1eefng l\u1ea7n migration n\u00e0o \u0111\u00e3 di\u1ec5n ra cho \u0111\u1ebfn th\u1eddi \u0111i\u1ec3m hi\u1ec7n t\u1ea1i, b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng l\u1ec7nh Artisan <\/span><span style=\"font-weight: 400;\">migrate:status<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">php artisan migrate:status<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng l\u1ec7nh <\/span><span style=\"font-weight: 400;\">php artisan migrate:rollback <\/span><span style=\"font-weight: 400;\">\u0111\u1ec3 quay l\u1ea1i phi\u00ean b\u1ea3n di chuy\u1ec3n tr\u01b0\u1edbc \u0111\u00f3 n\u1ebfu c\u1ea7n thi\u1ebft.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">N\u1ebfu b\u1ea1n mu\u1ed1n xem c\u00e1c c\u00e2u l\u1ec7nh SQL s\u1ebd \u0111\u01b0\u1ee3c th\u1ef1c thi b\u1edfi qu\u00e1 tr\u00ecnh migration m\u00e0 kh\u00f4ng c\u1ea7n ch\u1ea1y ch\u00fang, b\u1ea1n c\u00f3 th\u1ec3 cung c\u1ea5p flag <\/span><span style=\"font-weight: 400;\">&#8211;pretend<\/span><span style=\"font-weight: 400;\"> cho l\u1ec7nh <\/span><span style=\"font-weight: 400;\">migrate<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">php artisan migrate --pretend<\/span><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Laravel_migration_doi_voi_Table_bang\"><\/span><b> Laravel migration \u0111\u1ed1i v\u1edbi Table (b\u1ea3ng)<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>T\u1ea1o b\u1ea3ng<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 t\u1ea1o m\u1ed9t b\u1ea3ng database m\u1edbi, h\u00e3y s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">create<\/span><span style=\"font-weight: 400;\"> ph\u01b0\u01a1ng th\u1ee9c tr\u00ean <\/span><span style=\"font-weight: 400;\">schema<\/span><span style=\"font-weight: 400;\"> facade. Ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">create<\/span><span style=\"font-weight: 400;\"> ch\u1ea5p nh\u1eadn hai \u0111\u1ed1i s\u1ed1: \u0111\u1ed1i s\u1ed1 \u0111\u1ea7u ti\u00ean l\u00e0 t\u00ean c\u1ee7a b\u1ea3ng, \u0111\u1ed1i s\u1ed1 th\u1ee9 hai l\u00e0 m\u1ed9t closure \u0111\u00f3ng nh\u1eadn m\u1ed9t <\/span><span style=\"font-weight: 400;\">Blueprint<\/span><span style=\"font-weight: 400;\"> \u0111\u1ed1i t\u01b0\u1ee3ng c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 x\u00e1c \u0111\u1ecbnh b\u1ea3ng m\u1edbi:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Nh\u01b0 v\u00ed d\u1ee5 d\u01b0\u1edbi \u0111\u00e2y s\u1ebd t\u1ea1o b\u1ea3ng <\/span><span style=\"font-weight: 400;\">users<\/span> <span style=\"font-weight: 400;\">v\u1edbi c\u00e1c c\u1ed9t <\/span><span style=\"font-weight: 400;\">id<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">name<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">email<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">created_at<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">updated_at<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">use Illuminate\\Database\\Schema\\Blueprint;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">use Illuminate\\Support\\Facades\\Schema;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">Schema::create('users', function (Blueprint $table) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0$table-&gt;id();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0$table-&gt;string('name');<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0$table-&gt;string('email');<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0$table-&gt;timestamps();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">});<\/span><\/pre>\n<h3><b>C\u1eadp nh\u1eadt b\u1ea3ng<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Ph\u01b0\u01a1ng ph\u00e1p <\/span><span style=\"font-weight: 400;\">table<\/span><span style=\"font-weight: 400;\"> tr\u00ean <\/span><span style=\"font-weight: 400;\">schema<\/span><span style=\"font-weight: 400;\"> facade c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 c\u1eadp nh\u1eadt c\u00e1c b\u1ea3ng hi\u1ec7n c\u00f3. Gi\u1ed1ng nh\u01b0 ph\u01b0\u01a1ng ph\u00e1p <\/span><span style=\"font-weight: 400;\">create<\/span><span style=\"font-weight: 400;\">, ph\u01b0\u01a1ng ph\u00e1p <\/span><span style=\"font-weight: 400;\">table<\/span><span style=\"font-weight: 400;\"> n\u00e0y ch\u1ea5p nh\u1eadn hai \u0111\u1ed1i s\u1ed1: t\u00ean c\u1ee7a b\u1ea3ng v\u00e0 m\u1ed9t closure nh\u1eadn m\u1ed9t <\/span><span style=\"font-weight: 400;\">Blueprint<\/span><span style=\"font-weight: 400;\"> th\u1ec3 hi\u1ec7n m\u00e0 b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng \u0111\u1ec3 th\u00eam c\u00e1c c\u1ed9t ho\u1eb7c index v\u00e0o b\u1ea3ng:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">use Illuminate\\Database\\Schema\\Blueprint;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">use Illuminate\\Support\\Facades\\Schema;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">Schema::table('users', function (Blueprint $table) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0$table-&gt;integer('votes');<\/span>\r\n\r\n<span style=\"font-weight: 400;\">});<\/span><\/pre>\n<h3><b>\u0110\u1ed5i t\u00ean \/ X\u00f3a b\u1ea3ng<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 \u0111\u1ed5i t\u00ean b\u1ea3ng database, h\u00e3y s\u1eed d\u1ee5ng ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">rename<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">use Illuminate\\Support\\Facades\\Schema;\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">Schema::rename($from, $to);<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 x\u00f3a m\u1ed9t b\u1ea3ng hi\u1ec7n c\u00f3, b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng c\u00e1c ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">drop<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">dropIfExists<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">Schema::drop('users');<\/span>\r\n\r\n<span style=\"font-weight: 400;\">Schema::dropIfExists('users');<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 \u0111\u1ed5i t\u00ean b\u1ea3ng b\u1eb1ng foreign keys, b\u1ea1n x\u00e1c minh r\u1eb1ng m\u1ecdi r\u00e0ng bu\u1ed9c foreign keys tr\u00ean b\u1ea3ng \u0111\u1ec1u c\u00f3 t\u00ean r\u00f5 r\u00e0ng trong t\u1ec7p di chuy\u1ec3n thay v\u00ec \u0111\u1ec3 Laravel g\u00e1n t\u00ean d\u1ef1a tr\u00ean quy \u01b0\u1edbc. N\u1ebfu kh\u00f4ng, t\u00ean r\u00e0ng bu\u1ed9c foreign keys s\u1ebd tham chi\u1ebfu \u0111\u1ebfn t\u00ean b\u1ea3ng c\u0169.<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Laravel_migration_doi_voi_Column_Cot\"><\/span><b> Laravel migration \u0111\u1ed1i v\u1edbi Column (C\u1ed9t)<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>T\u1ea1o c\u1ed9t<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">table<\/span><span style=\"font-weight: 400;\"> tr\u00ean <\/span><span style=\"font-weight: 400;\">Schema<\/span><span style=\"font-weight: 400;\"> Facade c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 c\u1eadp nh\u1eadt c\u00e1c b\u1ea3ng hi\u1ec7n c\u00f3. Gi\u1ed1ng nh\u01b0 ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">create<\/span><span style=\"font-weight: 400;\">, ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">table<\/span><span style=\"font-weight: 400;\"> n\u00e0y ch\u1ea5p nh\u1eadn hai \u0111\u1ed1i s\u1ed1: t\u00ean c\u1ee7a b\u1ea3ng v\u00e0 m\u1ed9t closure nh\u1eadn m\u1ed9t <\/span><span style=\"font-weight: 400;\">Illuminate\\Database\\Schema\\Blueprint<\/span><span style=\"font-weight: 400;\"> th\u1ec3 hi\u1ec7n m\u00e0 b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng \u0111\u1ec3 th\u00eam c\u00e1c c\u1ed9t v\u00e0o b\u1ea3ng:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">use Illuminate\\Database\\Schema\\Blueprint;\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">use Illuminate\\Support\\Facades\\Schema; <\/span>\r\n\r\n\r\n<span style=\"font-weight: 400;\">Schema::table('users', function (Blueprint $table) {\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">$table-&gt;integer('votes');\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">});<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">B\u1ea3n thi\u1ebft k\u1ebf tr\u00ecnh t\u1ea1o schema cung c\u1ea5p nhi\u1ec1u ph\u01b0\u01a1ng th\u1ee9c t\u01b0\u01a1ng \u1ee9ng v\u1edbi c\u00e1c lo\u1ea1i c\u1ed9t kh\u00e1c nhau m\u00e0 b\u1ea1n c\u00f3 th\u1ec3 th\u00eam v\u00e0o b\u1ea3ng database c\u1ee7a m\u00ecnh. Chi ti\u1ebft c\u00e1c lo\u1ea1i c\u1ed9t, b\u1ea1n c\u00f3 th\u1ec3 xem trong file documentation t\u1ea1i <\/span><a href=\"https:\/\/laravel.com\/docs\/11.x\/migrations#generating-migrations\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">link n\u00e0y<\/span><\/a><span style=\"font-weight: 400;\">.<\/span><\/p>\n<h3><b>Column modifiers (C\u00f4ng c\u1ee5 s\u1eeda \u0111\u1ed5i c\u1ed9t)<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Ngo\u00e0i c\u00e1c lo\u1ea1i c\u1ed9t \u0111\u01b0\u1ee3c li\u1ec7t k\u00ea \u1edf tr\u00ean, c\u00f2n c\u00f3 m\u1ed9t s\u1ed1 c\u1ed9t &#8220;modifiers&#8221; m\u00e0 b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng khi th\u00eam c\u1ed9t v\u00e0o b\u1ea3ng c\u01a1 s\u1edf d\u1eef li\u1ec7u. V\u00ed d\u1ee5: \u0111\u1ec3 t\u1ea1o c\u1ed9t &#8220;nullable&#8221;, b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">nullable<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">use Illuminate\\Database\\Schema\\Blueprint;\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">use Illuminate\\Support\\Facades\\Schema;\u00a0<\/span>\r\n\r\n\r\n<span style=\"font-weight: 400;\">Schema::table('users', function (Blueprint $table) {\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">$table-&gt;string('email')-&gt;nullable();\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">});<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">B\u1ea3ng sau ch\u1ee9a t\u1ea5t c\u1ea3 c\u00e1c c\u1ed9t modifiers c\u00f3 s\u1eb5n. Danh s\u00e1ch n\u00e0y kh\u00f4ng bao g\u1ed3m c\u00e1c modifiers index:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Modifier<\/b><\/td>\n<td><b>M\u00f4 t\u1ea3<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">-&gt;after(&#8216;column&#8217;)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\u0110\u1eb7t c\u1ed9t &#8220;after&#8221; sau m\u1ed9t c\u1ed9t kh\u00e1c (MySQL)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">-&gt;autoIncrement()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\u0110\u1eb7t c\u00e1c c\u1ed9t INTEGER th\u00e0nh auto-incrementing (kh\u00f3a ch\u00ednh)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">-&gt;charset(&#8216;utf8mb4&#8217;)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ch\u1ec9 \u0111\u1ecbnh b\u1ed9 k\u00fd t\u1ef1 cho c\u1ed9t (MySQL)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">-&gt;collation(&#8216;utf8mb4_unicode_ci&#8217;)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ch\u1ec9 \u0111\u1ecbnh \u0111\u1ed1i chi\u1ebfu cho c\u1ed9t<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">-&gt;comment(&#8216;my comment&#8217;)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Th\u00eam nh\u1eadn x\u00e9t v\u00e0o m\u1ed9t c\u1ed9t (MySQL \/ PostgreSQL)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">-&gt;default($value)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ch\u1ec9 \u0111\u1ecbnh gi\u00e1 tr\u1ecb &#8220;default&#8221; cho c\u1ed9t<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">-&gt;first()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\u0110\u1eb7t c\u1ed9t &#8220;first&#8221; trong b\u1ea3ng (MySQL)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">-&gt;from($integer)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\u0110\u1eb7t gi\u00e1 tr\u1ecb b\u1eaft \u0111\u1ea7u c\u1ee7a tr\u01b0\u1eddng auto-incrementing (MySQL \/ PostgreSQL)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">-&gt;invisible()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">L\u00e0m c\u1ed9t &#8220;invisible&#8221; th\u00e0nh <\/span><span style=\"font-weight: 400;\">SELECT *<\/span><span style=\"font-weight: 400;\"> c\u00e1c truy v\u1ea5n (MySQL)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">-&gt;nullable($value = true)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Cho ph\u00e9p ch\u00e8n gi\u00e1 tr\u1ecb NULL v\u00e0o c\u1ed9t<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">-&gt;storedAs($expression)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">T\u1ea1o m\u1ed9t c\u1ed9t \u0111\u01b0\u1ee3c l\u01b0u tr\u1eef (MySQL \/ PostgreSQL \/ SQLite)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">-&gt;unsigned()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\u0110\u1eb7t c\u00e1c c\u1ed9t INTEGER l\u00e0 UNSIGNED (MySQL)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">-&gt;useCurrent()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\u0110\u1eb7t c\u1ed9t TIMESTAMP \u0111\u1ec3 s\u1eed d\u1ee5ng CURRENT_TIMESTAMP l\u00e0m gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">-&gt;useCurrentOnUpdate()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\u0110\u1eb7t c\u1ed9t TIMESTAMP \u0111\u1ec3 s\u1eed d\u1ee5ng CURRENT_TIMESTAMP khi b\u1ea3n ghi \u0111\u01b0\u1ee3c c\u1eadp nh\u1eadt (MySQL)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">-&gt;virtualAs($expression)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">T\u1ea1o m\u1ed9t c\u1ed9t \u1ea3o (MySQL \/ SQLite)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">-&gt;generatedAs($expression)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">T\u1ea1o m\u1ed9t c\u1ed9t danh t\u00ednh v\u1edbi c\u00e1c t\u00f9y ch\u1ecdn tr\u00ecnh t\u1ef1 \u0111\u01b0\u1ee3c ch\u1ec9 \u0111\u1ecbnh (PostgreSQL)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">-&gt;always()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">X\u00e1c \u0111\u1ecbnh th\u1ee9 t\u1ef1 \u01b0u ti\u00ean c\u1ee7a c\u00e1c gi\u00e1 tr\u1ecb chu\u1ed7i so v\u1edbi \u0111\u1ea7u v\u00e0o cho m\u1ed9t c\u1ed9t danh t\u00ednh (PostgreSQL)<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><b>S\u1eeda \u0111\u1ed5i c\u1ed9t<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Ph\u01b0\u01a1ng ph\u00e1p <\/span><span style=\"font-weight: 400;\">change<\/span><span style=\"font-weight: 400;\"> cho ph\u00e9p b\u1ea1n s\u1eeda \u0111\u1ed5i lo\u1ea1i v\u00e0 thu\u1ed9c t\u00ednh c\u1ee7a c\u00e1c c\u1ed9t hi\u1ec7n c\u00f3. V\u00ed d\u1ee5: b\u1ea1n c\u00f3 th\u1ec3 t\u0103ng k\u00edch th\u01b0\u1edbc c\u1ee7a m\u1ed9t c\u1ed9t <\/span><span style=\"font-weight: 400;\">string<\/span><span style=\"font-weight: 400;\">. \u0110\u1ec3 xem ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">change<\/span><span style=\"font-weight: 400;\"> ho\u1ea1t \u0111\u1ed9ng, h\u00e3y t\u0103ng k\u00edch th\u01b0\u1edbc c\u1ee7a <\/span><span style=\"font-weight: 400;\">name<\/span><span style=\"font-weight: 400;\"> c\u1ed9t t\u1eeb 25 l\u00ean 50. \u0110\u1ec3 th\u1ef1c hi\u1ec7n \u0111i\u1ec1u n\u00e0y, ch\u00fang ta ch\u1ec9 c\u1ea7n x\u00e1c \u0111\u1ecbnh tr\u1ea1ng th\u00e1i m\u1edbi c\u1ee7a c\u1ed9t v\u00e0 sau \u0111\u00f3 g\u1ecdi ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">change<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">Schema::table('users', function (Blueprint $table) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">$table-&gt;string('name', 50)-&gt;change();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">});<\/span><span style=\"font-weight: 400;\">\u00a0<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Khi s\u1eeda \u0111\u1ed5i m\u1ed9t c\u1ed9t, ph\u1ea3i bao g\u1ed3m r\u00f5 r\u00e0ng t\u1ea5t c\u1ea3 c\u00e1c modifiers m\u00e0 b\u1ea1n mu\u1ed1n gi\u1eef l\u1ea1i trong \u0111\u1ecbnh ngh\u0129a c\u1ed9t &#8211; m\u1ecdi thu\u1ed9c t\u00ednh b\u1ecb thi\u1ebfu s\u1ebd b\u1ecb lo\u1ea1i b\u1ecf. V\u00ed d\u1ee5: \u0111\u1ec3 gi\u1eef l\u1ea1i c\u00e1c thu\u1ed9c t\u00ednh <\/span><span style=\"font-weight: 400;\">unsigned<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">default<\/span><span style=\"font-weight: 400;\">, v\u00e0 <\/span><span style=\"font-weight: 400;\">comment<\/span><span style=\"font-weight: 400;\">, b\u1ea1n ph\u1ea3i g\u1ecdi t\u1eebng c\u00f4ng c\u1ee5 s\u1eeda \u0111\u1ed5i m\u1ed9t c\u00e1ch r\u00f5 r\u00e0ng khi thay \u0111\u1ed5i c\u1ed9t:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">Schema::table('users', function (Blueprint $table) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">$table-&gt;integer('votes')-&gt;unsigned()-&gt;default(1)-&gt;comment('my comment')-&gt;change();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">});<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">change<\/span><span style=\"font-weight: 400;\"> kh\u00f4ng thay \u0111\u1ed5i index c\u1ee7a c\u1ed9t. Do \u0111\u00f3, b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng modifiers index \u0111\u1ec3 th\u00eam ho\u1eb7c x\u00f3a index m\u1ed9t c\u00e1ch r\u00f5 r\u00e0ng khi s\u1eeda \u0111\u1ed5i c\u1ed9t:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">\/\/ Add an index...<\/span>\r\n\r\n<span style=\"font-weight: 400;\">$table-&gt;bigIncrements('id')-&gt;primary()-&gt;change();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\/\/ Drop an index...<\/span>\r\n\r\n<span style=\"font-weight: 400;\">$table-&gt;char('postal_code', 10)-&gt;unique(false)-&gt;change();<\/span><\/pre>\n<h3><b>Renaming Columns (\u0110\u1ed5i t\u00ean c\u1ed9t)<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 \u0111\u1ed5i t\u00ean m\u1ed9t c\u1ed9t, b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">renameColumn<\/span><span style=\"font-weight: 400;\"> do tr\u00ecnh t\u1ea1o schema cung c\u1ea5p:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">Schema::table('users', function (Blueprint $table) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">$table-&gt;renameColumn('from', 'to');<\/span>\r\n\r\n<span style=\"font-weight: 400;\">});<\/span><\/pre>\n<p><b>Dropping Columns<\/b><\/p>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 c\u1ed9t, b\u1ea1n s\u1eed d\u1ee5ng ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">dropColumn<\/span><span style=\"font-weight: 400;\"> do tr\u00ecnh t\u1ea1o schema cung c\u1ea5p:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">Schema::table('users', function (Blueprint $table) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">$table-&gt;dropColumn('votes');<\/span>\r\n\r\n<span style=\"font-weight: 400;\">});<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">B\u1ea1n c\u00f3 th\u1ec3 lo\u1ea1i b\u1ecf nhi\u1ec1u c\u1ed9t kh\u1ecfi m\u1ed9t b\u1ea3ng b\u1eb1ng c\u00e1ch chuy\u1ec3n m\u1ed9t m\u1ea3ng t\u00ean c\u1ed9t v\u00e0o ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">dropColumn<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">Schema::table('users', function (Blueprint $table) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">$table-&gt;dropColumn(['votes', 'avatar', 'location']);<\/span>\r\n\r\n<span style=\"font-weight: 400;\">});<\/span><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Laravel_migration_doi_voi_Index\"><\/span><b> Laravel migration \u0111\u1ed1i v\u1edbi Index<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>T\u1ea1o index<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Tr\u00ecnh x\u00e2y d\u1ef1ng schema Laravel h\u1ed7 tr\u1ee3 m\u1ed9t s\u1ed1 lo\u1ea1i index. V\u00ed d\u1ee5 sau t\u1ea1o m\u1ed9t c\u1ed9t <\/span><span style=\"font-weight: 400;\">email<\/span><span style=\"font-weight: 400;\"> m\u1edbi v\u00e0 ch\u1ec9 \u0111\u1ecbnh r\u1eb1ng c\u00e1c gi\u00e1 tr\u1ecb c\u1ee7a n\u00f3 ph\u1ea3i l\u00e0 index duy nh\u1ea5t (Unique index). \u0110\u1ec3 t\u1ea1o index, ch\u00fang ta c\u00f3 th\u1ec3 x\u00e2u chu\u1ed7i ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">unique<\/span><span style=\"font-weight: 400;\"> n\u00e0y v\u00e0o \u0111\u1ecbnh ngh\u0129a c\u1ed9t:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">use Illuminate\\Database\\Schema\\Blueprint;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">use Illuminate\\Support\\Facades\\Schema;<\/span>\r\n\r\n\r\n<span style=\"font-weight: 400;\">Schema::table('users', function (Blueprint $table) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">$table-&gt;string('email')-&gt;unique();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">});<\/span><span style=\"font-weight: 400;\">\u00a0<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Ngo\u00e0i ra, b\u1ea1n c\u00f3 th\u1ec3 t\u1ea1o index sau khi x\u00e1c \u0111\u1ecbnh c\u1ed9t. \u0110\u1ec3 l\u00e0m nh\u01b0 v\u1eady, b\u1ea1n n\u00ean g\u1ecdi ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">unique<\/span><span style=\"font-weight: 400;\"> tr\u00ean b\u1ea3n thi\u1ebft k\u1ebf tr\u00ecnh t\u1ea1o schema. Ph\u01b0\u01a1ng th\u1ee9c n\u00e0y ch\u1ea5p nh\u1eadn t\u00ean c\u1ee7a c\u1ed9t s\u1ebd nh\u1eadn \u0111\u01b0\u1ee3c index duy nh\u1ea5t:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">$table-&gt;unique('email');<\/span><span style=\"font-weight: 400;\">\u00a0<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">B\u1ea1n th\u1eadm ch\u00ed c\u00f3 th\u1ec3 chuy\u1ec3n m\u1ed9t m\u1ea3ng c\u00e1c c\u1ed9t sang m\u1ed9t ph\u01b0\u01a1ng th\u1ee9c index \u0111\u1ec3 t\u1ea1o index ph\u1ee9c h\u1ee3p (ho\u1eb7c t\u1ed5ng h\u1ee3p):<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">$table-&gt;index(['account_id', 'created_at']);<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Khi t\u1ea1o index, Laravel s\u1ebd t\u1ef1 \u0111\u1ed9ng t\u1ea1o t\u00ean index d\u1ef1a tr\u00ean b\u1ea3ng, t\u00ean c\u1ed9t v\u00e0 lo\u1ea1i index, nh\u01b0ng b\u1ea1n c\u00f3 th\u1ec3 chuy\u1ec3n \u0111\u1ed1i s\u1ed1 th\u1ee9 hai cho ph\u01b0\u01a1ng th\u1ee9c \u0111\u1ec3 t\u1ef1 ch\u1ec9 \u0111\u1ecbnh t\u00ean index:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">$table-&gt;unique('email', 'unique_email');<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">C\u00f3 nhi\u1ec1u lo\u1ea1i index ph\u00f9 h\u1ee3p v\u1edbi t\u1eebng nhu c\u1ea7u, \u0111\u1ec3 bi\u1ebft chi ti\u1ebft v\u00e0 ho\u00e0n ch\u1ec9nh c\u00e1c lo\u1ea1i Index types, b\u1ea1n c\u00f3 th\u1ec3 truy c\u1eadp <a href=\"https:\/\/laravel.com\/docs\/11.x\/migrations#generating-migrations\" target=\"_blank\" rel=\"noopener\">documentation c\u1ee7a Laravel<\/a>.<\/span><\/p>\n<h3><b>\u0110\u1ed5i t\u00ean index<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 \u0111\u1ed5i t\u00ean m\u1ed9t index, b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng ph\u01b0\u01a1ng ph\u00e1p <\/span><span style=\"font-weight: 400;\">renameIndex<\/span><span style=\"font-weight: 400;\"> do b\u1ea3n thi\u1ebft k\u1ebf tr\u00ecnh t\u1ea1o schema cung c\u1ea5p. Ph\u01b0\u01a1ng th\u1ee9c n\u00e0y ch\u1ea5p nh\u1eadn t\u00ean index hi\u1ec7n t\u1ea1i l\u00e0m \u0111\u1ed1i s\u1ed1 \u0111\u1ea7u ti\u00ean v\u00e0 t\u00ean mong mu\u1ed1n l\u00e0m \u0111\u1ed1i s\u1ed1 th\u1ee9 hai:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">$table-&gt;renameIndex('from', 'to')<\/span><span style=\"font-weight: 400;\">\u00a0<\/span><\/pre>\n<h3><b>X\u00f3a\/Gi\u1ea3m index<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 x\u00f3a index, b\u1ea1n ph\u1ea3i ch\u1ec9 \u0111\u1ecbnh t\u00ean c\u1ee7a index. Theo m\u1eb7c \u0111\u1ecbnh, Laravel t\u1ef1 \u0111\u1ed9ng g\u00e1n t\u00ean index d\u1ef1a tr\u00ean t\u00ean b\u1ea3ng, t\u00ean c\u1ed9t \u0111\u01b0\u1ee3c l\u1eadp index v\u00e0 lo\u1ea1i index. D\u01b0\u1edbi \u0111\u00e2y l\u00e0 m\u1ed9t s\u1ed1 v\u00ed d\u1ee5:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">N\u1ebfu b\u1ea1n chuy\u1ec3n m\u1ed9t m\u1ea3ng c\u1ed9t v\u00e0o m\u1ed9t ph\u01b0\u01a1ng th\u1ee9c lo\u1ea1i b\u1ecf index, t\u00ean index th\u00f4ng th\u01b0\u1eddng s\u1ebd \u0111\u01b0\u1ee3c t\u1ea1o d\u1ef1a tr\u00ean t\u00ean b\u1ea3ng, c\u1ed9t v\u00e0 lo\u1ea1i index:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">Schema::table('geo', function (Blueprint $table) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">$table-&gt;dropIndex(['state']); \/\/ Drops index 'geo_state_index'<\/span>\r\n\r\n<span style=\"font-weight: 400;\">});<\/span><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Events\"><\/span><b> Events<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 thu\u1eadn ti\u1ec7n s\u1eed d\u1ee5ng, m\u1ed7i ho\u1ea1t \u0111\u1ed9ng migration s\u1ebd g\u1eedi m\u1ed9t event. T\u1ea5t c\u1ea3 c\u00e1c event sau \u0111\u00e2y \u0111\u1ec1u m\u1edf r\u1ed9ng base class <\/span><span style=\"font-weight: 400;\">Illuminate\\Database\\Events\\MigrationEvent<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Class<\/b><\/td>\n<td><b>M\u00f4 t\u1ea3<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Illuminate\\Database\\Events\\MigrationsStarted<\/span><\/td>\n<td><span style=\"font-weight: 400;\">M\u1ed9t lo\u1ea1t l\u1ec7nh migration s\u1eafp \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Illuminate\\Database\\Events\\MigrationsEnded<\/span><\/td>\n<td><span style=\"font-weight: 400;\">M\u1ed9t lo\u1ea1t qu\u00e1 tr\u00ecnh migration \u0111\u00e3 \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n xong<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Illuminate\\Database\\Events\\MigrationStarted<\/span><\/td>\n<td><span style=\"font-weight: 400;\">M\u1ed9t cu\u1ed9c migration duy nh\u1ea5t s\u1eafp \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Illuminate\\Database\\Events\\MigrationEnded<\/span><\/td>\n<td><span style=\"font-weight: 400;\">M\u1ed9t l\u1ea7n migration duy nh\u1ea5t \u0111\u00e3 ho\u00e0n t\u1ea5t<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Illuminate\\Database\\Events\\NoPendingMigrations<\/span><\/td>\n<td><span style=\"font-weight: 400;\">L\u1ec7nh migration kh\u00f4ng t\u00ecm th\u1ea5y b\u1ea5t k\u1ef3 migration n\u00e0o \u0111ang ch\u1edd x\u1eed l\u00fd<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Illuminate\\Database\\Events\\SchemaDumped<\/span><\/td>\n<td><span style=\"font-weight: 400;\">K\u1ebft xu\u1ea5t database schema \u0111\u00e3 ho\u00e0n th\u00e0nh<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Illuminate\\Database\\Events\\SchemaLoaded<\/span><\/td>\n<td><span style=\"font-weight: 400;\">K\u1ebft xu\u1ea5t database schema hi\u1ec7n c\u00f3 \u0111\u00e3 \u0111\u01b0\u1ee3c t\u1ea3i<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><span class=\"ez-toc-section\" id=\"Cau_hoi_thuong_gap_ve_Laravel_migration\"><\/span><b>C\u00e2u h\u1ecfi th\u01b0\u1eddng g\u1eb7p v\u1ec1 Laravel migration<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b> C\u00f3 th\u1ec3 kh\u00f4i ph\u1ee5c qu\u00e1 tr\u00ecnh di chuy\u1ec3n c\u1ee7a Laravel kh\u00f4ng?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Kh\u00f4i ph\u1ee5c m\u1ecdi thay \u0111\u1ed5i b\u1eb1ng l\u1ec7nh <\/span><span style=\"font-weight: 400;\">php artisan migrate:rollback<\/span><span style=\"font-weight: 400;\"> nh\u01b0 b\u00ean d\u01b0\u1edbi:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">2019_08_19_000000_create_failed_jobs_table .......................................................................................... 1ms DONE<\/span>\r\n\r\n<span style=\"font-weight: 400;\">2019_05_10_000000_add_fields_to_action_events_table ................................................................................. 8ms DONE<\/span>\r\n\r\n<span style=\"font-weight: 400;\">2018_01_01_000000_create_action_events_table ........................................................................................ 1ms DONE<\/span>\r\n\r\n<span style=\"font-weight: 400;\">2014_10_12_100000_create_password_resets_table ...................................................................................... 1ms DONE<\/span>\r\n\r\n<span style=\"font-weight: 400;\">2014_10_12_000000_create_users_table ................................................................................................ 1ms DONE<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">H\u00e3y \u0111\u1ea3m b\u1ea3o s\u1eed d\u1ee5ng ph\u01b0\u01a1ng ph\u00e1p <\/span><span style=\"font-weight: 400;\">down()<\/span><span style=\"font-weight: 400;\"> m\u1ed9t c\u00e1ch ch\u00ednh x\u00e1c. Ph\u01b0\u01a1ng ph\u00e1p <\/span><span style=\"font-weight: 400;\">down()<\/span><span style=\"font-weight: 400;\"> ph\u1ea3i l\u00e0m ng\u01b0\u1ee3c l\u1ea1i v\u1edbi ph\u01b0\u01a1ng ph\u00e1p <\/span><span style=\"font-weight: 400;\">up()<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">use Illuminate\\Support\\Facades\\Schema;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">use Illuminate\\Database\\Schema\\Blueprint;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">use Illuminate\\Database\\Migrations\\Migration;<\/span>\r\n\r\n\r\n<span style=\"font-weight: 400;\">return new class extends Migration {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public function up()<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Schema::table('posts', function (Blueprint $table) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ The column was a boolean, but we want to switch to a datetime.<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$table-&gt;datetime('is_published')-&gt;nullable()-&gt;change();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0});<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public function down()<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Schema::table('posts', function (Blueprint $table) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ When rolling back, we have to restore the column to its previous state.<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$table-&gt;boolean('is_published')-&gt;default(false)-&gt;change();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0});<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<h3><b> C\u00f3 th\u1ec3 ch\u1ea1y Laravel migration khi \u0111ang ki\u1ec3m th\u1eed kh\u00f4ng?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">C\u00e2u tr\u1ea3 l\u1eddi l\u00e0 b\u1ea1n c\u00f3 th\u1ec3 ch\u1ea1y Laravel migration trong m\u00f4i tr\u01b0\u1eddng ki\u1ec3m th\u1eed b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng l\u1ec7nh ch\u1ec9 \u0111\u1ecbnh m\u00f4i tr\u01b0\u1eddng th\u1eed nghi\u1ec7m: <\/span><span style=\"font-weight: 400;\">php artisan migrate &#8211;env=testing<\/span><\/p>\n<h3><b> C\u00f3 th\u1ec3 s\u1eeda \u0111\u1ed5i c\u00e1c c\u1ed9t hi\u1ec7n c\u00f3 trong qu\u00e1 tr\u00ecnh di chuy\u1ec3n c\u1ee7a Laravel kh\u00f4ng?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">B\u1ea1n c\u00f3 th\u1ec3 s\u1eeda \u0111\u1ed5i c\u00e1c c\u1ed9t hi\u1ec7n c\u00f3 trong Laravel migration b\u1eb1ng h\u00e0m <\/span><span style=\"font-weight: 400;\">change<\/span><span style=\"font-weight: 400;\"> c\u1ee7a ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">table<\/span><span style=\"font-weight: 400;\">. Sau \u0111\u00e2y l\u00e0 v\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">public function up()<\/span>\r\n\r\n<span style=\"font-weight: 400;\">{<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0Schema::table('your_table_name', function (Blueprint $table) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$table-&gt;string('new_column_name')-&gt;change();<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0});<\/span>\r\n\r\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<h3><b> T\u1ea1i sao n\u00ean s\u1eed d\u1ee5ng Migrate?<\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">D\u1ec5 d\u00e0ng qu\u1ea3n l\u00fd c\u1ea5u tr\u00fac database: Migrate cung c\u1ea5p giao di\u1ec7n \u0111\u01a1n gi\u1ea3n \u0111\u1ec3 t\u1ea1o, s\u1eeda \u0111\u1ed5i v\u00e0 x\u00f3a c\u00e1c y\u1ebfu t\u1ed1 database, gi\u00fap b\u1ea1n d\u1ec5 d\u00e0ng qu\u1ea3n l\u00fd c\u1ea5u tr\u00fac database c\u1ee7a \u1ee9ng d\u1ee5ng.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Ki\u1ec3m so\u00e1t phi\u00ean b\u1ea3n: Migrate s\u1eed d\u1ee5ng h\u1ec7 th\u1ed1ng t\u1eadp tin \u0111\u1ec3 l\u01b0u tr\u1eef c\u00e1c thay \u0111\u1ed5i c\u1ea5u tr\u00fac database, gi\u00fap b\u1ea1n d\u1ec5 d\u00e0ng theo d\u00f5i v\u00e0 qu\u1ea3n l\u00fd l\u1ecbch s\u1eed thay \u0111\u1ed5i.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">H\u1ee3p t\u00e1c: Nhi\u1ec1u Developer c\u00f3 th\u1ec3 l\u00e0m vi\u1ec7c c\u00f9ng nhau tr\u00ean c\u1ea5u tr\u00fac database c\u1ee7a \u1ee9ng d\u1ee5ng b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng Migrate.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Gi\u1ea3m l\u1ed7i: Migrate gi\u00fap gi\u1ea3m thi\u1ec3u l\u1ed7i do vi\u1ec7c s\u1eeda \u0111\u1ed5i c\u1ea5u tr\u00fac database th\u1ee7 c\u00f4ng.<\/span><\/li>\n<\/ul>\n<blockquote><p><em>\u0110\u1ecdc th\u00eam: <a href=\"https:\/\/itviec.com\/blog\/tai-lieu-hoc-laravel\/\" target=\"_blank\" rel=\"noopener\"><strong>Top 18 t\u00e0i nguy\u00ean h\u1ecdc Laravel hay nh\u1ea5t n\u0103m 2024<\/strong><\/a><\/em><\/p><\/blockquote>\n<h3><b>C\u00e1c lo\u1ea1i Migrate ph\u1ed5 bi\u1ebfn<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">C\u00f3 hai lo\u1ea1i Migrate ph\u1ed5 bi\u1ebfn:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Up migrations: T\u1ea1o ho\u1eb7c s\u1eeda \u0111\u1ed5i c\u00e1c y\u1ebfu t\u1ed1 database.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Down migrations: Ho\u00e0n t\u00e1c c\u00e1c thay \u0111\u1ed5i \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n b\u1edfi Up migrations.<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Tong_ket_Laravel_migration\"><\/span><b>T\u1ed5ng k\u1ebft Laravel migration<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 th\u1ea5y, Laravel migration gi\u00fap c\u00e1c Developer \u0111\u01a1n gi\u1ea3n h\u00f3a vi\u1ec7c qu\u1ea3n l\u00fd database trong c\u00e1c \u1ee9ng d\u1ee5ng Laravel. B\u1eb1ng c\u00e1ch cung c\u1ea5p kh\u1ea3 n\u0103ng ki\u1ec3m so\u00e1t phi\u00ean b\u1ea3n, di chuy\u1ec3n, h\u1ed7 tr\u1ee3 c\u1ed9ng t\u00e1c, kh\u00f4i ph\u1ee5c v\u00e0 c\u1ea3i thi\u1ec7n t\u00ednh nh\u1ea5t qu\u00e1n. Nh\u1eefng l\u1ee3i \u00edch n\u00e0y g\u00f3p ph\u1ea7n n\u00e2ng cao qu\u00e1 tr\u00ecnh ph\u00e1t tri\u1ec3n, gi\u00fap \u1ee9ng d\u1ee5ng hi\u1ec7u qu\u1ea3 v\u00e0 \u0111\u00e1ng tin c\u1eady h\u01a1n. Hy v\u1ecdng r\u1eb1ng v\u1edbi nh\u1eefng th\u00f4ng tin ITviec v\u1eeba chia s\u1ebb, b\u1ea1n \u0111\u00e3 n\u1eafm r\u00f5 c\u00e1ch t\u1ea1o, kh\u1edfi ch\u1ea1y v\u00e0 s\u1eed d\u1ee5ng Laravel migration hi\u1ec7u qu\u1ea3 nh\u1ea5t.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Laravel migration l\u00e0 c\u00f4ng c\u1ee5 qu\u1ea3n l\u00fd c\u01a1 s\u1edf d\u1eef li\u1ec7u hi\u1ec7u qu\u1ea3, t\u1eeb vi\u1ec7c t\u1ea1o b\u1ea3ng \u0111\u1ebfn thay \u0111\u1ed5i c\u1ea5u tr\u00fac. V\u1edbi c\u00e1c c\u00e2u l\u1ec7nh PHP \u0111\u01a1n gi\u1ea3n, Developer c\u00f3 th\u1ec3 t\u1ef1 tin th\u1ef1c hi\u1ec7n thay \u0111\u1ed5i m\u00e0 kh\u00f4ng \u1ea3nh h\u01b0\u1edfng \u0111\u1ebfn d\u1eef li\u1ec7u hi\u1ec7n t\u1ea1i. B\u00e0i vi\u1ebft n\u00e0y s\u1ebd h\u01b0\u1edbng d\u1eabn b\u1ea1n t\u1eebng [&hellip;]<\/p>\n","protected":false},"author":205,"featured_media":75785,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_gspb_post_css":"","footnotes":""},"categories":[109],"tags":[],"class_list":["post-75695","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-chuyen-mon-it"],"blocksy_meta":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.8 (Yoast SEO v27.7) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Laravel Migration: H\u01b0\u1edbng d\u1eabn \u00e1p d\u1ee5ng migration trong Laravel - ITviec Blog<\/title>\n<meta name=\"description\" content=\"Laravel migration l\u00e0 c\u00f4ng c\u1ee5 qu\u1ea3n l\u00fd c\u01a1 s\u1edf d\u1eef li\u1ec7u. B\u00e0i vi\u1ebft h\u01b0\u1edbng d\u1eabn s\u1eed d\u1ee5ng Laravel migration, t\u1eeb kh\u1edfi t\u1ea1o cho \u0111\u1ebfn c\u00e1c thay \u0111\u1ed5i ph\u1ee9c t\u1ea1p.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/itviec.com\/blog\/laravel-migration-la-gi\/\" \/>\n<meta property=\"og:locale\" content=\"vi_VN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Laravel Migration: H\u01b0\u1edbng d\u1eabn \u00e1p d\u1ee5ng migration trong Laravel\" \/>\n<meta property=\"og:description\" content=\"Laravel migration l\u00e0 c\u00f4ng c\u1ee5 qu\u1ea3n l\u00fd c\u01a1 s\u1edf d\u1eef li\u1ec7u hi\u1ec7u qu\u1ea3, t\u1eeb vi\u1ec7c t\u1ea1o b\u1ea3ng \u0111\u1ebfn thay \u0111\u1ed5i c\u1ea5u tr\u00fac. V\u1edbi c\u00e1c c\u00e2u l\u1ec7nh PHP \u0111\u01a1n gi\u1ea3n, Developer c\u00f3 th\u1ec3 t\u1ef1\" \/>\n<meta property=\"og:url\" content=\"https:\/\/itviec.com\/blog\/laravel-migration-la-gi\/\" \/>\n<meta property=\"og:site_name\" content=\"ITviec Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ITviec\" \/>\n<meta property=\"article:published_time\" content=\"2024-07-21T14:36:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/07\/Laravel-migration-thumbnail-vippro.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1500\" \/>\n\t<meta property=\"og:image:height\" content=\"790\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"H\u00e0 My\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ITviec\" \/>\n<meta name=\"twitter:site\" content=\"@ITviec\" \/>\n<meta name=\"twitter:label1\" content=\"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi\" \/>\n\t<meta name=\"twitter:data1\" content=\"H\u00e0 My\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc\" \/>\n\t<meta name=\"twitter:data2\" content=\"15 ph\u00fat\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Laravel Migration: H\u01b0\u1edbng d\u1eabn \u00e1p d\u1ee5ng migration trong Laravel - ITviec Blog","description":"Laravel migration l\u00e0 c\u00f4ng c\u1ee5 qu\u1ea3n l\u00fd c\u01a1 s\u1edf d\u1eef li\u1ec7u. B\u00e0i vi\u1ebft h\u01b0\u1edbng d\u1eabn s\u1eed d\u1ee5ng Laravel migration, t\u1eeb kh\u1edfi t\u1ea1o cho \u0111\u1ebfn c\u00e1c thay \u0111\u1ed5i ph\u1ee9c t\u1ea1p.","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:\/\/itviec.com\/blog\/laravel-migration-la-gi\/","og_locale":"vi_VN","og_type":"article","og_title":"Laravel Migration: H\u01b0\u1edbng d\u1eabn \u00e1p d\u1ee5ng migration trong Laravel","og_description":"Laravel migration l\u00e0 c\u00f4ng c\u1ee5 qu\u1ea3n l\u00fd c\u01a1 s\u1edf d\u1eef li\u1ec7u hi\u1ec7u qu\u1ea3, t\u1eeb vi\u1ec7c t\u1ea1o b\u1ea3ng \u0111\u1ebfn thay \u0111\u1ed5i c\u1ea5u tr\u00fac. V\u1edbi c\u00e1c c\u00e2u l\u1ec7nh PHP \u0111\u01a1n gi\u1ea3n, Developer c\u00f3 th\u1ec3 t\u1ef1","og_url":"https:\/\/itviec.com\/blog\/laravel-migration-la-gi\/","og_site_name":"ITviec Blog","article_publisher":"https:\/\/www.facebook.com\/ITviec","article_published_time":"2024-07-21T14:36:32+00:00","og_image":[{"width":1500,"height":790,"url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/07\/Laravel-migration-thumbnail-vippro.jpg","type":"image\/jpeg"}],"author":"H\u00e0 My","twitter_card":"summary_large_image","twitter_creator":"@ITviec","twitter_site":"@ITviec","twitter_misc":{"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi":"H\u00e0 My","\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc":"15 ph\u00fat"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/itviec.com\/blog\/laravel-migration-la-gi\/#article","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/laravel-migration-la-gi\/"},"author":{"name":"H\u00e0 My","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/7bd099db44e5079508c9b7e8c0161e3c"},"headline":"Laravel Migration: H\u01b0\u1edbng d\u1eabn \u00e1p d\u1ee5ng migration trong Laravel","datePublished":"2024-07-21T14:36:32+00:00","mainEntityOfPage":{"@id":"https:\/\/itviec.com\/blog\/laravel-migration-la-gi\/"},"wordCount":3941,"publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"image":{"@id":"https:\/\/itviec.com\/blog\/laravel-migration-la-gi\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/07\/Laravel-migration-thumbnail-vippro.jpg","articleSection":["Chuy\u00ean m\u00f4n IT"],"inLanguage":"vi"},{"@type":"WebPage","@id":"https:\/\/itviec.com\/blog\/laravel-migration-la-gi\/","url":"https:\/\/itviec.com\/blog\/laravel-migration-la-gi\/","name":"Laravel Migration: H\u01b0\u1edbng d\u1eabn \u00e1p d\u1ee5ng migration trong Laravel - ITviec Blog","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/itviec.com\/blog\/laravel-migration-la-gi\/#primaryimage"},"image":{"@id":"https:\/\/itviec.com\/blog\/laravel-migration-la-gi\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/07\/Laravel-migration-thumbnail-vippro.jpg","datePublished":"2024-07-21T14:36:32+00:00","description":"Laravel migration l\u00e0 c\u00f4ng c\u1ee5 qu\u1ea3n l\u00fd c\u01a1 s\u1edf d\u1eef li\u1ec7u. B\u00e0i vi\u1ebft h\u01b0\u1edbng d\u1eabn s\u1eed d\u1ee5ng Laravel migration, t\u1eeb kh\u1edfi t\u1ea1o cho \u0111\u1ebfn c\u00e1c thay \u0111\u1ed5i ph\u1ee9c t\u1ea1p.","breadcrumb":{"@id":"https:\/\/itviec.com\/blog\/laravel-migration-la-gi\/#breadcrumb"},"inLanguage":"vi","potentialAction":[{"@type":"ReadAction","target":["https:\/\/itviec.com\/blog\/laravel-migration-la-gi\/"]}]},{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/laravel-migration-la-gi\/#primaryimage","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/07\/Laravel-migration-thumbnail-vippro.jpg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/07\/Laravel-migration-thumbnail-vippro.jpg","width":1500,"height":790,"caption":"laravel migration - itviec blog"},{"@type":"BreadcrumbList","@id":"https:\/\/itviec.com\/blog\/laravel-migration-la-gi\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Chuy\u00ean m\u00f4n IT","item":"https:\/\/itviec.com\/blog\/chuyen-mon-it\/"},{"@type":"ListItem","position":2,"name":"Laravel Migration: H\u01b0\u1edbng d\u1eabn \u00e1p d\u1ee5ng migration trong Laravel"}]},{"@type":"WebSite","@id":"https:\/\/itviec.com\/blog\/#website","url":"https:\/\/itviec.com\/blog\/","name":"ITviec Blog","description":"IT Jobs &amp; People in Vietnam","publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/itviec.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"vi"},{"@type":"Organization","@id":"https:\/\/itviec.com\/blog\/#organization","name":"ITviec","url":"https:\/\/itviec.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2018\/12\/itviec-black-square-facebook.png","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2018\/12\/itviec-black-square-facebook.png","width":1800,"height":1800,"caption":"ITviec"},"image":{"@id":"https:\/\/itviec.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ITviec","https:\/\/x.com\/ITviec","https:\/\/www.linkedin.com\/company\/itviec","https:\/\/www.youtube.com\/channel\/UCYthAQ3bcGr57M_ag5gHDvQ"]},{"@type":"Person","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/7bd099db44e5079508c9b7e8c0161e3c","name":"H\u00e0 My","image":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/ha-my-author-e1709882319892-100x100.jpeg","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/ha-my-author-e1709882319892-100x100.jpeg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/ha-my-author-e1709882319892-100x100.jpeg","caption":"H\u00e0 My"},"url":"https:\/\/itviec.com\/blog\/author\/ha-my\/"}]}},"_links":{"self":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/75695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/users\/205"}],"replies":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/comments?post=75695"}],"version-history":[{"count":0,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/75695\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media\/75785"}],"wp:attachment":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media?parent=75695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/categories?post=75695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/tags?post=75695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}