Simple blog application in laravel 5 Part-1: Setup database

NOTE:

If you face any problem then please don't ask question on demo website (in test posts). Please use our forum http://www.findalltogether.com/forum/laravel/. You will get email when there will be any response to your question.

All parts of this series:

Simple blog application in laravel 5 - Introduction

Simple blog application in laravel 5 (part-1): Setup database

Simple blog application in laravel 5 (part-2): Routes and Models

Simple blog application in laravel 5 (part-3): Controllers

Simple blog application in laravel 5 (part-4): Build Front end

Simple blog application in laravel 5 (part-5): Add tinyMCE and User profile

In this article we will setup database tables for storing data. We will use migrations and schema builder for generating tables.

First config database by changing .env file. .env file is hidden file in root folder of application (here in blog folder).

  1. DB_HOST=localhost
  2. DB_DATABASE=your_database_name
  3. DB_USERNAME=database_username
  4. DB_PASSWORD=database_password

Set your database in blog >> config >> database.php by changing value of default:

  1. <?php
  2. return [
  3. ...
  4. ...
  5. // set database
  6. 'default' => 'mysql',
  7. ...
  8. ...

For more configurations read Database configuration in laravel 5.

Now make migrations, using commands:

  1. php artisan make:migration posts
  2. php artisan make:migration comments

You will get two more files in blog >> database >> migrations with postfix posts.php and comments.php. Note that there were two files already there. The first file with name 2014_10_12_000000_create_users_table.php is generating table for users in database. Another file with name 2014_10_12_100000_create_password_resets_table.php generates table for password reset.

Create table for posts:

Open file that is created by migration with suffix posts.php in migrations folder and paste this code. This code is creating a table named posts in database.

  1. <?php
  2. use Illuminate\Database\Schema\Blueprint;
  3. use Illuminate\Database\Migrations\Migration;
  4. class Posts extends Migration {
  5. /**
  6. * Run the migrations.
  7. *
  8. * @return void
  9. */
  10. public function up()
  11. {
  12. // blog table
  13. Schema::create('posts', function(Blueprint $table)
  14. {
  15. $table->increments('id');
  16. $table -> integer('author_id') -> unsigned() -> default(0);
  17. $table->foreign('author_id')
  18. ->references('id')->on('users')
  19. ->onDelete('cascade');
  20. $table->string('title')->unique();
  21. $table->text('body');
  22. $table->string('slug')->unique();
  23. $table->boolean('active');
  24. $table->timestamps();
  25. });
  26. }
  27. /**
  28. * Reverse the migrations.
  29. *
  30. * @return void
  31. */
  32. public function down()
  33. {
  34. // drop blog table
  35. Schema::drop('posts');
  36. }
  37. }

Migration is laravel core class which is used for database version control. Migration class provides two functions up() and down(). up() function executes when migration runs. down() function undo the up() function and runs when we rollback last migration or reset the database.

Schema is another laravel class which create and modify database tables.

  1. Schema::create('posts', function(Blueprint $table)
  2. {
  3. $table->increments('id');
  4. $table -> integer('author_id') -> unsigned() -> default(0);
  5. $table->foreign('author_id')
  6. ->references('id')->on('users')
  7. ->onDelete('cascade');
  8. $table->string('title')->unique();
  9. $table->text('body');
  10. $table->string('slug')->unique();
  11. $table->boolean('active');
  12. $table->timestamps();
  13. });

In this code we are calling create() function which takes two arguments. First is the database table name on which we are operating and another is the closure which is creating columns in this table. $table is instance to this table. Laravel provides built-in functions for column types in database. For example, increments('id') is creating a columns with name 'id' and which is a primary key (automatic increases).

For detailed knowledge read migrations and schema builder.

Create table for comments:

Similarly paste this code in file with suffix commets.php in migrations folder.

  1. <?php
  2. use Illuminate\Database\Schema\Blueprint;
  3. use Illuminate\Database\Migrations\Migration;
  4. class Comments extends Migration {
  5. /**
  6. * Run the migrations.
  7. *
  8. * @return void
  9. */
  10. public function up()
  11. {
  12. //id, on_blog, from_user, body, at_time
  13. Schema::create('comments', function(Blueprint $table)
  14. {
  15. $table->increments('id');
  16. $table -> integer('on_post') -> unsigned() -> default(0);
  17. $table->foreign('on_post')
  18. ->references('id')->on('posts')
  19. ->onDelete('cascade');
  20. $table -> integer('from_user') -> unsigned() -> default(0);
  21. $table->foreign('from_user')
  22. ->references('id')->on('users')
  23. ->onDelete('cascade');
  24. $table->text('body');
  25. $table->timestamps();
  26. });
  27. }
  28. /**
  29. * Reverse the migrations.
  30. *
  31. * @return void
  32. */
  33. public function down()
  34. {
  35. // drop comment
  36. Schema::drop('comments');
  37. }
  38. }

User table:

Migration for user table is already written in 2014_10_12_000000_create_users_table.php file. But we also want to add roles to the user. So just add one line in this file:

  1. $table->string('password', 60);
  2. // add this one line
  3. $table->enum('role',['admin','author','subscriber'])->default('author');
  4. $table->rememberToken();

Migrate to database:

Generate tables in database by using this command:

  1. php artisan migrate

This command will generate all tables in database. Till now we have setup tables in the database for our application. After migration try this link http://localhost:8000/auth/login



About Harish Kumar

Harish is an interested person in the field of web development and blogging. He works for the need of young web developers in learning various languages, latest technologies and other essential tips and tricks. If you need some help or you have some suggestion then you email him at harish@findalltogether.com without any hesitation. You can also suggest/demand for articles of your own choice.

Related Articles

Laravel is a MVC framework. Models are the real world entities in data form. Views are the response content to the vi...
Laravel framework architecture
Routes map urls with the controllers. In this section we will read about how route maps urls with controllers and how...
Basics routing and controllers in laravel 5
Views are the HTML content served by the server. These are served by returning from controllers. Views are .php or .b...
View in Laravel 5

Login or Sign up to leave comment.


2 Comments

  • need
    hello i just download ur source code , i setup in my laptop then i register/login why i cant see option to create post ? just have view profile and logout
    December 5, 2015 at 01:12 PM Reply

    Login or Sign up to reply.

    • Harish Kumar

      After login you will get option for creating new posts in menu in top-right side. Please can you share some screenshots on harish@findalltogether.com

      December 7, 2015 at 12:12 PM