Laravel TDD Session – Create An Application in Laravel – Part 1

Laravel TDD Session - Create An Application in Laravel With TDD - Part 1

Introduction

Hi, Laravel TDD or Test Driven Development with Laravel is a hot topic today. This is the first article in the series of Laravel TDD. If you are new to Laravel TDD and want to get start. Then you are at right place.

If you are beginner in Laravel and don’t know where to start. You can read me previous article Laravel Beginners Session With MySQL and MongoDB – Part 1. This will help you setup a fresh Laravel application with step by step process.

If you are an intermediate or experienced Laravel developer and want to know how to get start Test Driven Development or TDD in Laravel. You can get start here.

Hard code developer section

Make a fresh Laravel installation

$ composer create-project --prefer-dist laravel/laravel forum

Change directory to project root

$ cd forum

Create a model, migration and resourceful controller

$ php artisan make:model Thread -mr

Now, go to thread migration and edit up() function with following code

public function up()
 {
     Schema::create('threads', function (Blueprint $table) {
         $table->increments('id');
         $table->integer('user_id');
         $table->string('title');
         $table->text('body');
         $table->timestamps();
     });
 }

Optional Section

Install Laravel 5 IDE Helper Generator

$ composer require --dev barryvdh/laravel-ide-helperlaravel

Install doctrine/dbal package

$ composer require doctrine/dbal

Add following code to register() method of app/Providers/AppServiceProvider.php

public function register()
 {
     if ($this->app->environment() !== 'production') {
         $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
     }
 }

Continue From Here…

Open MySQL database and create a new database called forum.

$ create database forum

Open .env file from Laravel project and edit your database configuration. Go to terminal and run migration

$ php artisan migrate

Create a model Reply from your terminal by running following command:

$ php artisan make:model Reply -mr

Open migration file of replies and modify up() method with following code:

public function up()
{
    Schema::create('replies', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('thread_id');
        $table->integer('user_id');
        $table->text('body');
        $table->timestamps();
    });
}

Run migration with following command:

$ php artisan migrate

Open database/factories/ModelFactory.php and add following code:

$factory->define(\App\Thread::class, function ($faker) {
    return [
        'user_id' => function () {
            return factory(\App\User::class)->create()->id;
        },
        'title' => $faker->sentence,
        'body' => $faker->paragraph
    ];
});

Laravel Tinker – Your New Friend

Open terminal and run following command:

$ php artisan tinker

It will give you access to Laravel’s tinker command. We shall use tinker for generating seeding data for our threads model. Run following command:

$ factory('App\Thread', 50);

It will fill 50 users and thread records in your database.

Add following code to your ModalFactory.php

$factory->define(\App\Reply::class, function ($faker) {
    return [
        'thread_id' => function () {
            return factory(\App\Thread::class)->create()->id;
        },
        'user_id' => function () {
            return factory(\App\User::class)->create()->id;
        },
        'body' => $faker->paragraph
    ];
});

Refresh your migration

$ php artisan migrate:refresh

This will delete all the seed data generated in previous step. So now run tinker below commands to generate Users, Threads and Replies

$ php artisan tinker
$ thread = factory('App\Thread', 50)->create(); 
$ $thread->each(function ($thread) { factory('App\Reply', 10)->create(['thread_id' => $thread->id]); });

First command will get you into Laravel tinker.

Second command will generate 50 users and threads in database and will assign thread object to $thread.

Third command will generate 10 replies for each thread.

This is the first part of series. We have a Laravel application with database setup and running. We will continue this in next post as we go along.

If you have any questions, post in comments section. If you like it share it with your friends and team.

Laravel Beginners Session With MySQL and MongoDB

Laravel Beginners Session

Introduction

Hi this is my first post. I am going to talk about Laravel. In this multi part series I am going to develop an application for myself. You can follow along to get started.

Who am I?

I am professional Web developer and Blogger. I have different blogs and websites in Laravel. Some are live some are in development. When I get free time I do WordPress stuff.  I watch movies a lot. Enough talking about me. Lets get back to work.

What are we going to do?

In this post I am going to take you through all the steps to setup a Laravel application with MySQL as well as MongoDB. If you have any questions, feel free to post on comments or ask on my Facebook page. I will try to reply as much possible.

What is Laravel?

Before getting into coding, lets talk a bit about what is Laravel. Laravel is a PHP framework and most popular one at present. It’s based on MVC design pattern. It have many inbuilt functionality out of the box that keeps you away from re-inventing the wheel.

Lets Start

Time to do coding stuff. I am going to write all commands for Linux Ubuntu 16.04 as I am using same right now. If you don’t have one you can do on any platform it will be working with same commands on Windows, Linux and Mac. So, it really doesn’t matter, feel free to do it.

Installation

Go to composer, and download and install composer if you don’t have it on your system. If you find it tough to install composer, you can follow this awesome digital ocean guide.
Install Laravel by using composer in your web directory of Ubuntu, which is by default /var/www/html/. Run following command to install Laravel.

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

Here blog is project name. Change it to whatever you like. If you get permission error you should run this command as sudo. It will take a while and will download all the dependencies and files for Laravel. Once it’s done, change directory to the project directory

$ cd blog

Now you can run your application by typing:

$ php artisan serv

This will run your Laravel application. You can see it running in your browser by typing at http://localhost:8000

Congratulations! You have done the first step and you have a full Laravel application running on your system. In my next post I shall be talking about database connection with MySQL and MongoDB. Keep visiting. If you have questions, use below comment section.

If you like this post, do share it with your friends and team.