WordPress-Like Blog Laravel 5.6 and AdminLTE 3 (3) – Create Dummy Data

In this third part of creating WordPress-Like Blog using Laravel 5.6 and AdminLTE 3, we will :

  1. Create Post model and posts table
  2. Prepare some dummy data using database seeder

Before we proceed, we need to make sure the database information inside .env file has been properly set according to your database credentials :

1. Create Post Model and posts table

Open terminal, and type:

The command will automatically create Post model and table posts through -m migration command.

Open database/migration folder, find the newly created migration file, then let’s fill the migration file that we had just created as follows:

The author_id column is made to store the user id information, who wrote the post.

Run the migration by typing :

Open your database administrator (PhpMyAdmin or Sequel Pro or Navicat), the posts table will appear.

2. Prepare Dummy Data using Database Seeder

We can manually insert data through database administrator like phpmyadmin, sequel pro, or navicat. However, we will be using database seed to fill our posts table. Let’s start by creating PostsTableSeeder and UsersTableSeeder.

UsersTableSeeder.php 

We have created three users from UsersTableSeeder.

PostsTableSeeder.php

We use Faker\Factory library to create dummy data. Faker create mock up strings such as sentences, words, even paragraphs.

Modify DatabaseSeeder.php, take note that UsersTableSeeder::class should be placed before PostsTableSeeder.

Next step is to generate the seeder. Open your terminal and type:

This command will generate the seeder. Now check your users and posts table. They have filled with dummy data successfully.

Github Part 3

3 thoughts on “WordPress-Like Blog Laravel 5.6 and AdminLTE 3 (3) – Create Dummy Data

  • when i run:

    php artisan migrate

    i get this error:

    Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table posts add constraint posts_author_id_foreign foreign key (author_id) references users (id) on delete restrict)

    at /var/www/html/becauseican/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
    660| // If an exception occurs when attempting to run a query, we’ll format the error
    661| // message to include the bindings with SQL, which will make this exception a
    662| // lot more helpful to the developer instead of just the database’s errors.
    663| catch (Exception $e) {
    > 664| throw new QueryException(
    665| $query, $this->prepareBindings($bindings), $e
    666| );
    667| }
    668|

    Exception trace:

    1 PDOException::(“SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint”)
    /var/www/html/becauseican/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458

    2 PDOStatement::execute()
    /var/www/html/becauseican/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458

    can you please help?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.