Twitter From Scratch Using Laravel: Initial Database Setup


In our first episode we sort of talking about the introduction for this series.  In this second epesode we are going to do some initial database setup. In the process we will learn some Laravel features such as database seeding, migrations, and model factories.

First, let's create a database and we will name it twitter. Please take note I'm using Laravel Homestead as my local development environment. So, I'm going to log into my homestead environment first using vagrant ssh.
D:\Homestead>vagrant ssh
...
vagrant@homestead:~$ mysql
...
mysql> create database twitter;

Next, let's edit our database credentials in our .env file.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=twitter
DB_USERNAME=homestead
DB_PASSWORD=secret

Next, let's create a tweets table migration using make:migration artisan command.
php artisan make:migration create_tweets_table --create=tweets

Next, let's edit database/migrations/2017_03_29_015136_create_tweets_table.php to add some column in our tweets table. For now let's just add user_id column for the id of the user who owns the tweet and body column for the body of the tweet.
...
public function up()
{
    Schema::create('tweets', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('user_id');
        $table->string('body');
        $table->timestamps();
    });
}

Next, let's run migrate artisan command to add the tweets table in our database.
php artisan migrate

Next, let's create a model for our tweets table using make:model artisan command.
php artisan make:model Tweet
This command will create a Tweet.php file under the app directory.

Next, let's create a model factory for our Tweet model in database/factories/ModelFactory.php.
...

$factory->define(App\Tweet::class, function (Faker\Generator $faker) {
    return [
        'user_id' => function () {
            return factory(App\User::class)->create()->id;
        },
        'body' => $faker->sentence
    ];
});
Notice how we create a user_id for our tweet. We're using the user factory that is included in Laravel out of the box. So, basically every time we run factory(App\Tweet::class)->create() we're also creating a new user in our users table.

Next, let's create a table seeder for our tweets table using make:seeder artisan command.
php artisan make:seeder TweetsTableSeeder
This will create a TweetsTableSeeder.php file under database/seeds directory.

Next, let's edit that TweetsTableSeeder.php file and use our Tweet model factory for seeding our tweets table.
...

public function run()
{
    factory(App\Tweet::class, 50)->create();
}
This will create 50 random tweets in our tweets table and 50 random users in our users table.

Next, let's add our tweets table seeder to database/seeds/DatabaseSeeder.php which is the one being executed if a seeder is not specified in the db:seed artisan command.
...

public function run()
{
    $this->call(TweetsTableSeeder::class);
}

Next, let's run the seeder using db:seed artisan command.
php artisan db:seed

As I mentioned you can specify what seeder you want to run.
php artisan db:seed --class=TweetsTableSeeder

Now that our database is ready, in our next episode we are going start working on some features. If you have any questions or suggestions please don't hesitate to write it down in the comment below. Stay tuned.

View the source code for this lesson on GitHub.

Previous: IntroductionNext: Our First Test