This tutorial continues where part one left off.
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.
Laravel supports four databases;
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.
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;
Replace db_name, db_user, and db_password appropriately.
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.
Use the migrate artisan command to run all your outstanding migrations.
php artisan migrate
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
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
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
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.
To create database seeders in Laravel, you use the make:seeder artisan command.
php artisan make:seeder PostSeeder
This will create a seeder class boilerplate in the database/seeders directory.
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.
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 in Laravel are used to automatically generate dummy data that can be used to populate the database for testing purposes.
To create database factories in, you use the make:factory artisan command.
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.