Set Up & Seed Laravel Database
Laravel, MariaDB, MySQL
These instructions are for local development of Laravel on Ubuntu 16.04 Xenial Xerus (Desktop). The instrauctions have been tested on 10.1.16-MariaDB, but they should also work for MySQL.
Create Database
Create a Database with a unique user:
Connect Laravel
Enter the Databse connection details in .env
:
Test the Database
Laravel comes with a couple of migrations defined out of the box.
To check that Laravel is properly connected to the new database:
Open a new terminal and run the pending Laravel migrations:
This will run the default migrations (under Laravel 5.3, a “users” table and a “password_resets” table will be created, as well as a “migrations” table).
You can also seed the database. Create a seeder class:
This will create database/seeds/UsersTableSeeder.php
, which will be stubbed out with an empty run()
method.
This method can be used to insert records into the database. Laravel includes the handy Faker
factory class, which allows dummy data to be created.
To determine which fields to seed, check the table:
Next, amend the run()
method of the seeder class that you’ve just created:
Now, when you run:
…the seeder will create 25 dummy users complete with realistic data.
Note: running php artisan db:seed
will only run the default DatabaseSeeder
class - which will be empty. This is a bit different to the php artisan migrate
command, which runs all migrations under the database/migrations
directory.
To check the seeder results, run the following command from the mysql prompt:
Note that Laravel has a much better way of interacting with your database on the fly - Artisan tinker:
comments powered by Disqus