Writing your first Laravel app - Part Two

Writing your first Laravel app - Part Two

Rai Omido

This tutorial continues where part one left off.

# Introduction

This tutorial is a continuation of tutorial one.  We'll set up the database, create your first model, and get a quick introduction to Laravel's database seeds.

# Database: Getting Started

Laravel supports four databases;

  • MySQL 5.6+
  • PostgreSQL 9.4+
  • SQLite 3.8.8+
  • SQL Server 2017+

For this tutorial, we shall be using the MySQL database. If you would use any other database type, you may visit the Laravel Database Documentation.

~Create a new database for our application

If you have installed XAMPP, just start the XAMPP control panel and start MySQL and Apache.

After that, visit http://localhost/phpmyadmin in your browser. It will open the PHPMyAdmin dashboard, where you can create your database and user.

Now, open up the .env file in your project's root directory. It's a configuration file with variables representing Laravel settings.

Enter your values in the .env file as follows;

DB_DATABASE=db_name
DB_USERNAME=db_user
DB_PASSWORD=db_password

Replace db_name, db_user, and db_password appropriately.

# Database Migrations

Migrations in Laravel are files that are used to manage the database.

They can be used to create, update, and delete database tables and columns.

The easiest way to create a database migration in Laravel is by using the make:migration artisan command.

php artisan make:migration create_posts_table

This will create a migration boilerplate in the database/migrations directory.

~Running Migrations

Use the migrate artisan command to run all your outstanding migrations.

php artisan migrate
~Rolling back migrations

Use the rollback artisan command to roll back the latest migrations.

php artisan migrate:rollback

You may use the migrate:reset command to roll back all your application's migrations.

php artisan migrate:reset
~Roll Back & Migrate Using A Single Command

Use the migrate:refresh command to roll back all of your application's migrations and then execute the migrate command.

php artisan migrate:refresh

// Refresh the database and run all database seeds...
php artisan migrate:refresh --seed
~Drop All Tables & Migrate

The fresh artisan command will drop all the tables in the database and then execute the migrate command.

PHP artisan migrate:fresh
php artisan migrate:fresh --seed
~More on migrations

Up to this point, we have only managed to cover what is essential for this tutorial. You can read the exhaustive documentation on the Laravel Website.

# Database seeders

Database seeders in Laravel are classes that are used to populate the database with data.

It can be dummy data for testing or default data that is required by the app. E.g. the initial/superuser that will be responsible for creating other users.

~Creating seeders

To create database seeders in Laravel, you use the make:seeder artisan command.

For instance;

php artisan make:seeder PostSeeder

This will create a seeder class boilerplate in the database/seeders directory.

# Models

Each database table has a corresponding "Model" which is used to interact with that table.

Models are the core of Laravel's Eloquent ORM  which is a simple ActiveRecord implementation for working with your database

Models provide powerful functionality useful for creating, updating, retrieving, and deleting database records.

You can extend their functionality as per your requirements.

You can also define your own custom methods inside model classes.

~Creating Models

To create models, you use the make:model artisan command.

php artisan make:model User

This will create a model class boilerplate in the app/Models directory.

# Model Factories

Model factories in Laravel are used to automatically generate dummy data that can be used to populate the database for testing purposes.

~Creating Model Factories

To create database factories in, you use the make:factory artisan command.

For instance;

php artisan make:factory UserFactory

This will create a factory boilerplate in the database/factories directory.

You can read more about database testing using factories on the Laravel Website.

# Conclusion

In the next lesson, we will try out the database API and put some records in the database. You may proceed to Writing your first Laravel app - Part Three