Laravel in Practice - Setup .env - Database - Migrations

Laravel PHP Framework

 

 

Initial Setup of Environment File

The .env file in the root of your Laravel installation contains your settings. We will worry about the second section, and it should look something like this:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel54_database_name
DB_USERNAME=db_username_goes_here
DB_PASSWORD=password_goes_here

Create a new migration

This will create a new migration file in /databases/migrations/ for creating a tasks table.

php artisan make:migration create_tasks_table --create tasks

Running the above command will create a new file in your /database/migrations/ directory which should look similar to this:


use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

class CreateTasksTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('tasks');
}
}

Adding a new field

I want to create a text field called body, so here is how I would do it. I would update my PHP migration file that was just created.

 public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->increments('id');
$table->text('body');
$table->timestamps();
});
}

PHP Artisan Migrate

This will create all your database tables. This will run all your migrations and any new migrations. 

php artisan migrate

Migrate Refresh Redo

If you need to reset your migrations, you can run this command. It will check your migration files and redo the migrations.

php artisan migrate:refresh

 

What's do we work on next with Laravel 5.4?

Check out Laracasts - Laravel from Scratch PHP learning videos to watch someone demonstrate these commands.

 

Visit sunny St. George, Utah, USA