Building Advance Blog on Laravel 5.5 with TDD mode #1 prepare environment and database
Tags: phpdevelopment, php, laraveleloquent, laravel5
Published: 2017-11-26 13:02:46

---
title: "Building Advance Blog on Laravel 5.5 with TDD mode #1 prepare environment and database"
published: true
tags: php-development,php,laravel-eloquent,laravel-5
canonical_url: https://medium.com/lordofdev/building-advance-blog-on-laravel-5-5-with-tdd-mode-1-prepare-environment-and-database-7adcfa5fcb9a
---

![](https://cdn-images-1.medium.com/max/1024/1*mKvFjSmRchikZ4NDki74Mw.png)

hey guy this first post in this series I’ve to show you how to build a blog with cool feature like Activity feed, Filter post, post subscription, mentioned a user, notification, search with Algolia,&nbsp;etc

and browser not necessary much 80% of workers are on the terminal with test-driven development mode

this series I’ve to use cloud9 web IDE because it’s easy to setup system let we focus on the development

if you need to try cloud9 signup in this form I’ve added to my university account this gives the ability to clone my&nbsp;project

<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fdocs.google.com%2Fforms%2Fd%2Fe%2F1FAIpQLSeGsBdKQRYUifdf2V61BJ1sSJ2uh2-oQJIthKwVqJsp7nOBpg%2Fviewform%3Fembedded%3Dtrue&amp;url=https%3A%2F%2Fdocs.google.com%2Fforms%2Fd%2Fe%2F1FAIpQLSeGsBdKQRYUifdf2V61BJ1sSJ2uh2-oQJIthKwVqJsp7nOBpg%2Fviewform%3Fusp%3Dsend_form&amp;image=https%3A%2F%2Flh6.googleusercontent.com%2F2RyoEp8xBNBGSpIxPFJ2nbe-TYYSHphzUqRI1qy2UQQwdOIFUJI9tf2JAr55wQai-kQ%3Dw1200-h630-p&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=google" width="760" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/bae5e45a2ae7ed9997b9075f8efdbf2f/href">https://medium.com/media/bae5e45a2ae7ed9997b9075f8efdbf2f/href</a></iframe>> **Important note&nbsp;: if you need help from me signup to this form then I’ve sent invitation to&nbsp;email**

if you decide to start a new project from scratch follow this goto [c9.io/new](http://c9.io/new)

![](https://cdn-images-1.medium.com/max/1024/1*o8tlTQCwYq3KF2OrK5o_-g.png)

fill form look like this image and don’t forget to choose Team because I’ve easily take your&nbsp;issue

and then create a workspace

wait a minute because you have a free&nbsp;user

![](https://cdn-images-1.medium.com/max/1024/1*HtA8-G-QPvLHyuw6ZFhCYQ.png)

when workspace successfully loaded. You’ve seen this window left hand is file manager large area is editor area can open file or browser. the bottom is terminal or command&nbsp;line

doesn’t take you learning curve&nbsp;anymore.

just let’s get&nbsp;start

### upgrade php&nbsp;version

currently php is version 5.5.9 but laravel 5.5 require PHP \>= 7.0.0&nbsp;. so that we need to upgrade php&nbsp;first

![](https://cdn-images-1.medium.com/max/696/1*k4He0fBfbir1l5bQ8Vxi8w.png)

copy command below and paste to&nbsp;terminal

```
sudo add-apt-repository ppa:ondrej/php -y ; sudo apt-get update -y ; sudo apt-get install php7.0-curl php7.0-cli php7.0-dev php7.0-gd php7.0-intl php7.0-mcrypt php7.0-json php7.0-mysql php7.0-opcache php7.0-bcmath php7.0-mbstring php7.0-soap php7.0-xml php7.0-zip -y ; sudo mv /etc/apache2/envvars /etc/apache2/envvars.bak ; sudo apt-get remove libapache2-mod-php5 -y ; sudo apt-get install libapache2-mod-php7.0 -y ; sudo cp /etc/apache2/envvars.bak /etc/apache2/envvars
```

like this and&nbsp;wait…..

if succeed you’ve got php -v&nbsp;again

![](https://cdn-images-1.medium.com/max/931/1*QW5KsEk9HwTa4I9m6kz5rQ.png)

### Install Laravel

this is ubuntu docker image composer install ready then you can use&nbsp;command

```
composer create-project --prefer-dist laravel/laravel blog
```

if everything work&nbsp;fine

![](https://cdn-images-1.medium.com/max/1024/1*cpKpaWXv2Uae8g_wRh7kXw.png)

you’ve see this and&nbsp;this

![](https://cdn-images-1.medium.com/max/161/1*2HS8HG58Ns-gumQTyXPmPg.png)

then I’don’t like work on subfolder so that we move all in blog folder to&nbsp;root

![](https://cdn-images-1.medium.com/max/191/1*x56N3JiTjrTNs7_W_wodPQ.png)

look more comfortable

### Prepare database

let’s talk first if we are in testing mode I’ve don’t use mysql we don’t need it in testing mode we use&nbsp;Sqlite

#### install Sqlite

```
sudo apt-get install php7.0-sqlite
```

![](https://cdn-images-1.medium.com/max/789/1*tCynrBmfgTrSl6OcmIxs8A.png)

and test with type&nbsp;sqlite

![](https://cdn-images-1.medium.com/max/488/1*ByR5mbvugvhlxzF0JokNrQ.png)

#### .env for&nbsp;testing

next we rename&nbsp;.env.example to&nbsp;.env.testing then open and change APP\_ENV from local to testing and change DB\_CONNECTION to sqlite last remove red highlight

![](https://cdn-images-1.medium.com/max/691/1*Aam__d4k4FnzfFn-VAyg3Q.png)

next we test this file is work with generate&nbsp;key

php artisan key:generate --env=testing specify testing environment in parameter

![](https://cdn-images-1.medium.com/max/704/1*CpaXeOP-nPxRN-6ewRSiHQ.png)

that still&nbsp;work

![](https://cdn-images-1.medium.com/max/698/1*9kiIICq0JTbBwxgSsPA3eg.png)

#### create SQLite&nbsp;database

by default in config/database.php sqlite config use database name database.sqlite and store in database folder so we follow&nbsp;this

![](https://cdn-images-1.medium.com/max/1024/1*P3AxZdbqbve-jR-PzIVlAQ.png)

in terminal type touch database/database.sqlite then file instant&nbsp;appear

![](https://cdn-images-1.medium.com/max/188/1*VliErbiEs_lbPsVEsF9yNg.png)

next

when we go to build blog this table we need&nbsp;is

- User
- Post
- Comment

and a relation is&nbsp;easy

- User has many Post and&nbsp;Comment
- Post has many&nbsp;Comment

Laravel have already User model and migration so we get start in Post&nbsp;model

#### Post model and migration

type php artisan make:model Post -m for create model and migration

![](https://cdn-images-1.medium.com/max/512/1*ZqaRxgtjthK3HHhcFVZ1yA.png)

a file still appears in folder migration

![](https://cdn-images-1.medium.com/max/1024/1*qjK1phMElgOuGbyRhQ6R3A.png)

and when we look in app folder we see Post&nbsp;model

![](https://cdn-images-1.medium.com/max/996/1*tjgiWg7P_4ca7JhMKu5JdQ.png)

well next we create a table with migration

back to migration file

insert three&nbsp;column

```
$table->string('title',50);
            $table->integer('user_id');
            $table->text('body');
```

in this&nbsp;position

![](https://cdn-images-1.medium.com/max/814/1*LOIVt5n3xfli980_ra3nVQ.png)

then type php artisan migrate --env=testing

![](https://cdn-images-1.medium.com/max/531/1*-bjU5nu77Hw8RCCWHNDnhA.png)

cd to database folder then open sqlite shell sqlite3 database.sqlite

.tables show all&nbsp;table

![](https://cdn-images-1.medium.com/max/580/1*xFmiZL6D1YtEFRozUaEFmg.png)

now we already posts and users&nbsp;table

#### Comment model&nbsp;,migration and controller

we still lack comment table so create table&nbsp;, migration and controller too&nbsp;. cd to root directory and type command php artisan make:model Comment&nbsp;-mc

![](https://cdn-images-1.medium.com/max/626/1*i06M_bdx0AOx5bND1iqUGg.png)

-mc is optional parameter for tell artisan generate migration and controller file

look up to root directory, You’ve&nbsp;see

![](https://cdn-images-1.medium.com/max/372/1*Y33Cg-MmGGNXTFgrAHipKw.png)

and open migration file once again we insert three&nbsp;column

![](https://cdn-images-1.medium.com/max/740/1*6qJE7xNuVHNH4unKtfNCdw.png)

save and php artisan migrate --env=testing

![](https://cdn-images-1.medium.com/max/660/1*gfWSjKtCzOrrmHGQrmRmAg.png)

look up in database&nbsp;again

![](https://cdn-images-1.medium.com/max/503/1*KLIkFEkcC8gx_-pRBn1LBg.png)

let’s finish with seed data with factories

#### Seed data with factories

open UserFactory.php and fill this&nbsp;function

```
$factory->define(App\Post::class, function (Faker $faker) {

return [
        'user_id' => function(){
            return factory('App/User')->creat()->id;
        },
        'title' => $faker->sentence,
        'body' => $faker->paragraph
    ];
});
```

we bond post table column and faker method together and user\_id with user&nbsp;faker

like this

![](https://cdn-images-1.medium.com/max/1024/1*gYlCa4uNg54rfjr3rJUWiQ.png)

then goto terminal and type php artisan tinker --env=testing and generate post data 50 rows with factory('App\Post',50)-\>create()

![](https://cdn-images-1.medium.com/max/1024/1*aRMRAZFaaZE6EDKS7nt8vQ.png)

now we look to a table for make&nbsp;sure

![](https://cdn-images-1.medium.com/max/1024/1*FOiVOBM94y9JNWu7DYNe_Q.png)<figcaption>data from post table</figcaption>

for this command will generate user data&nbsp;too

![](https://cdn-images-1.medium.com/max/1024/1*yq8i1DFv7DBXC6mr_T0RpA.png)

well next we generate comment&nbsp;data

open **Userfactory.php** again and copy Post factory and paste&nbsp;it

then we modify **Post** to **Comment** and replace **title** with&nbsp; **post\_id**

```
$factory->define(App\Comment::class, function (Faker $faker) {

return [
        'user_id' => function(){
            return factory('App\User')->create()->id;
        },
        'post_id' => function(){
            return factory('App\Post')->create()->id;
        },
        'body' => $faker->paragraph
    ];

});
```

like this

![](https://cdn-images-1.medium.com/max/1024/1*-wHiFK44cfzmMpvwhZiQ7Q.png)

now we need to reseed data in database because when generate **Comment** that’s will generate **User** and **Post** concurrently with php artisan migrate:refresh --env=testing

that’ result

![](https://cdn-images-1.medium.com/max/744/1*QnwY8cqSC1nsSU1NIDqIdw.png)

ok back to **artisan tinker** again this situation different from&nbsp;before

```
$post = factory('App\Post',50)->create()
```

we use $post for assign data from&nbsp;factory

![](https://cdn-images-1.medium.com/max/846/1*KBRduymlPSorYI0RBk1nVA.png)

when enter it’s generate data and assign to&nbsp;$post

next we use $post for generate comment&nbsp;data

```
$post->each(function($post){ 
     factory(‘App\Comment’,10)->create([‘post_id’=>$post->id]);
 });
```

we use $post loop and generate 10 comment per post and add foreign&nbsp;key

![](https://cdn-images-1.medium.com/max/1024/1*2_tdswNV1xD8Y_-pEk_uWg.png)

ok that’s&nbsp;work

and we complete in the first&nbsp;chapter

**if you like this please give me 50 claps and Hit follow below that encouragement to move&nbsp;forward**

**this** [**project Github repository**](https://github.com/krissnawat/advance-blog-in-laravel-5.5-in-tdd) **you can star&nbsp;it**

> [next episode #2 prepare first test check out&nbsp;here](https://medium.com/lordofdev/building-advance-blog-on-laravel-5-5-with-tdd-mode-2-prepare-the-first-test-80c4687cfca6)
* * *