Skip to main content

Database

  • env file
  • migrations

env file

  • database connection variables are located in the env file
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mydbname
DB_USERNAME=root
DB_PASSWORD=123456

migrations

  • stored in the database/migrations folder
  • Laravel Migration is an essential feature in Laravel that allows you to create a table in your database.
  • It allows you to modify and share the application's database schema. You can modify the table by adding a new column or deleting an existing column.
  • to add a column to the users table
php artisan make:migration add_favcolor_to_user_table
a new file is created inside the database/migrations folder, open and edit it
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddColumnUserTable extends Migration{
public function up(){
Schema::table('users', function($table){$table->string('favcolor');});
}
public function down(){
Schema::table('users', function($table){$table->dropColumn('favcolor')});
}
}
  • to commit changes to the actual database
php artisan migrate //run migrations to update database