How to Create Tables With Laravel Migrations ?
Create a Migration:
Open your terminal and run the command
php artisan make:migration create_table_name_table
Replace table_name
with your desired table name.
Define the Table Structure
Locate the migration file in the database/migrations
directory. Open it and modify the up
method to define your table structure.
public function up()
{
$schema::create('table_name', function (Blueprint $table) {
$table->id(); //Creates an auto-incrementing primary key
$table->string('column_name'); //Example of a string column
$table->integer('another_column'); //Example of an integer column
$table->timestamps();
});
}
Define the down
method to drop the table if the migration is rolled back
public function down()
{
Schema::dropIfExists('table_name');
}
Run the Migrations : To create table in your database, run the command
php artisan migrate
After running the migration, check your database to confirm that the table has been created successfully.