DEV Community

0xAirdropfarmer
0xAirdropfarmer

Posted on • Originally published at Medium on

Building Advance Blog on Laravel 5.5 with TDD mode #1 prepare environment and database

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, 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 project

https://medium.com/media/bae5e45a2ae7ed9997b9075f8efdbf2f/href> Important note : if you need help from me signup to this form then I’ve sent invitation to email

if you decide to start a new project from scratch follow this goto c9.io/new

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

and then create a workspace

wait a minute because you have a free user

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 line

doesn’t take you learning curve anymore.

just let’s get start

upgrade php version

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

copy command below and paste to 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
Enter fullscreen mode Exit fullscreen mode

like this and wait…..

if succeed you’ve got php -v again

Install Laravel

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

composer create-project --prefer-dist laravel/laravel blog
Enter fullscreen mode Exit fullscreen mode

if everything work fine

you’ve see this and this

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

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 Sqlite

install Sqlite

sudo apt-get install php7.0-sqlite
Enter fullscreen mode Exit fullscreen mode

and test with type sqlite

.env for testing

next we rename .env.example to .env.testing then open and change APP_ENV from local to testing and change DB_CONNECTION to sqlite last remove red highlight

next we test this file is work with generate key

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

that still work

create SQLite database

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

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

next

when we go to build blog this table we need is

  • User
  • Post
  • Comment

and a relation is easy

  • User has many Post and Comment
  • Post has many Comment

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

Post model and migration

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

a file still appears in folder migration

and when we look in app folder we see Post model

well next we create a table with migration

back to migration file

insert three column

$table->string('title',50);
            $table->integer('user_id');
            $table->text('body');
Enter fullscreen mode Exit fullscreen mode

in this position

then type php artisan migrate --env=testing

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

.tables show all table

now we already posts and users table

Comment model ,migration and controller

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

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

look up to root directory, You’ve see

and open migration file once again we insert three column

save and php artisan migrate --env=testing

look up in database again

let’s finish with seed data with factories

Seed data with factories

open UserFactory.php and fill this 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
    ];
});
Enter fullscreen mode Exit fullscreen mode

we bond post table column and faker method together and user_id with user faker

like this

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

now we look to a table for make sure

data from post table

for this command will generate user data too

well next we generate comment data

open Userfactory.php again and copy Post factory and paste it

then we modify Post to Comment and replace title with  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
    ];

});
Enter fullscreen mode Exit fullscreen mode

like this

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

ok back to artisan tinker again this situation different from before

$post = factory('App\Post',50)->create()
Enter fullscreen mode Exit fullscreen mode

we use $post for assign data from factory

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

next we use $post for generate comment data

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

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

ok that’s work

and we complete in the first chapter

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

this project Github repository you can star it

next episode #2 prepare first test check out here


Top comments (1)

Collapse
 
leslieeeee profile image
Leslie

If you are a macOS user, ServBay.dev is worth to try. You don't need to spend some time or couple of days to setup anything. Just download it and you can use it immediately. You can run multiple PHP versions simultaneously and switch between them effortlessly.
Honestly, this tool has greatly simplified my PHP development and is definitely worth trying!