php-laravelHow do I add a column with a Laravel migration in PHP?
To add a column with a Laravel migration in PHP, you can use the addColumn method on the Schema facade. This method takes two parameters: the name of the table to add the column to, and an array of column properties.
For example, to add a column called name to the users table, you can use the following code:
Schema::table('users', function ($table) {
$table->string('name');
});
The array of column properties can include the column type, length, default value, and any other relevant options. For example, to add a price column with a default value of 0 to the products table, you can use the following code:
Schema::table('products', function ($table) {
$table->decimal('price', 8, 2)->default(0);
});
Code explanation
Schema: the facade for interacting with the databasetable: the method used to specify the table to add the column tostring/decimal: the type of column to add8, 2: the length of the column (for a decimal column)default: the method used to specify a default value for the column
Helpful links
More of Php Laravel
- ¿Cómo configurar PHP y Laravel desde cero?
- How do I set up a .gitlab-ci.yml file for a Laravel project using PHP?
- How do I configure Xdebug in the php.ini file for a Laravel project?
- How can I use the @yield directive in PHP Laravel?
- How can I create a website using the Laravel PHP framework and a template?
- How can I use PHP Laravel's ZipArchive library to create a zip file?
- How can I use Xdebug to debug a Laravel application written in PHP?
- How can I use the Laravel WhereIn method in PHP?
- How can I access an undefined array key in PHP Laravel?
- How do I run a seeder in Laravel using PHP?
See more codes...