Video tutorial: connect Laravel to database

Hi guys! In this tutorial I will explain to you how you can easily connect your Laravel project to database.

So, to connect your Laravel project to database:

  • Go to your  app\config folder and open your database.php file
  • Next, you must add database's name, username and password:

'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database name',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),

Now, I will explain how you can create migrations in Laravel. Migrations are a type of version control for your database. They allow a team to modify the database schema and stay up to date on the current schema state.

To create a database migration, open command line in from your Laravel project and enter this code:

php artisan migrate:make create_users_table

Next, find the generated migration file in the app/database/migrations folder. And add this code:

public function up()
{
Schema::create('users', function($table)
{
$table->increments('id');
$table->string('email')->unique();
$table->string('name');
$table->timestamps();
});
}

public function down()
{
Schema::drop('users');
}

Then, enter php artisan migrate in your command line and it will create two tables in your database.

Video tutorial:









Leave a Comment