📘Database Connectivity and Migration
🔗 1. Database Configuration (Connectivity)
Edit your .env file:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=your_db_name DB_USERNAME=root DB_PASSWORD=
🔍 2. Testing Database Connection
php artisan migrate
This will show if the connection is working by running migrations.
🛠️ 3. Creating a Migration
php artisan make:migration create_users_table
🧾 4. Migration File Example
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamps();
    });
}
public function down()
{
    Schema::dropIfExists('users');
}
  
  ⚙️ 5. Running Migrations
php artisan migratephp artisan migrate:refreshphp artisan migrate:rollbackphp artisan migrate:reset
✏️ 6. Modifying Tables
php artisan make:migration add_age_to_users_table
public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->integer('age')->nullable();
    });
}
public function down()
{
    Schema::table('users', function (Blueprint $table) {
        $table->dropColumn('age');
    });
}
  
  📌 7. Summary Table
| Command | Description | 
|---|---|
| php artisan migrate | Run all migrations | 
| php artisan migrate:rollback | Rollback last batch | 
| php artisan migrate:refresh | Reset and rerun all migrations | 
| php artisan migrate:reset | Reset all migrations | 
| php artisan make:migration | Create new migration | 
0 Comments