KodeBLOG

Kode Blog - Inspiring And Empowering Developers.

Home About Us Courses Categories Blog Contact Us

Homestead Laravel Hello World

Introduction

In the previous tutorial, we successfully created configured a vagrant box and installed Laravel homestead. We will pick up from where we left and create a Hello World project.

By the end of this tutorial, you would have known how to create a Laravel project in homestead and map it to shared directories in your host machine.

Homestead Hello World

Topics to be covered

In this tutorial, we will cover the following topics.

  • Tutorial Prerequisites
  • Homestead Create New Project
  • Homestead add new site
  • Homestead Database
  • Working on the code.
  • Connect to Homestead Database with remote client (MySQL Workbench)

Tutorial Prerequisites

The following prerequisites will help you to successfully complete this tutorial.

  • You should be familiar with the basics of virtualization, vagrant and Laravel homestead. If you are not familiar with these concepts, then read the previous tutorial
  • You should be familiar with vagrant common commands. Read the tutorial xxx if you are not familiar with the commands.
  • Basic knowledge of the command line will be an added advantage
  • Basic knowledge is Linux commands will be an added advantage
  • You have installed, virtual box, vagrant and Git.
  • You will need a text editor

Homestead Create New Project

We will create a new project from our vagrant machine. Open Git bash / terminal.

For now, we have configured vagrant to run from ~/Homestead directory.

Run the following command to browse to the homestead directory

cd ~/Homestead

letís now start homestead box assuming it is not running

vagrant up

we can now connect via ssh using the following command

vagrant ssh

Once logged in, you can browse to the Code directory

Run the following command

cd Code

We are now ready to create a new project. Run the following composer command

composer create-project laravel/laravel helloworld.app

HERE,

  • The above composer command creates a new Laravel project in a directory helloworld.app

Remember, the Code directory is a shared and synced with the mapped directory ~/Code on the host machine. When the above command has successfully been executed, you will be able to the corresponding on the host machine

Homestead add new site

We will now open the hosts file to configure our new site. In Mac / Linux, itís in /etc/hosts while on Windows itís in C:\Windows\System32\drivers\etc\hosts.

Note: on windows, we will use notepad to edit hosts and we will have it open it as an administrator as shown in the image below.

Open notepas as administrator

Add the following entry to the hosts file

192.168.10.10 helloworld.app

We will now edit the ~/.homestead/Homestead.yaml to map the site between the host and virtual box. We will also map the directories.

Add the following lines to the sites: definition

- map: helloworld.app
  to: /home/vagrant/Code/helloworld.app/public

Homestead database

For simplicityís sake, we will match the database name to our application.

Add the following line to the databases: section

- helloworld

Save the changes.

We will now need to reload and provision our machine so that homestead can pick them up.

Run the following command

vagrant reload Ėprovision

load the following URL in your web browser?

http://helloworld.app/

you should be able to see the following

Homestead Hello World

Use the comments section to ask for help if you do not get the desired results.

Working on the code

We created the project in homestead but we have a shared directory on the host Machine. For our hello world app, the code is in ~/Code/helloworld.app

You can now work on the code using your favourite development tools on your host machine. Letís edit the welcome template.

Open ~/Code/helloworld.app/resources/views/welcome.blade.php

We will change the text Laravel to Homestead Hello World.

Save the changes.

Reload the following URL

http://helloworld.app/

You should be able to see the following

Homestead Hello World

What just happened?

The changes we made to the code on the host machine where synced to the code in the virtual machine.

Go back to Git bash vagrant session

Letís browse to the resources directory of helloworld.app

Run the following command

cd ~/Code/helloworld.app/resources/views/

letís now display the contents of welcome.blade.php

cat welcome.blade.php

Homestead Code Synchronization

As you can see from the above results, homestead provides you with a cool development environment without getting in the way of your favourite tools and development machine.

Connect to Homestead Database with remote client (MySQL Workbench)

Remember we configured a database in Homestead.yaml file.

By default, we are working with MySQL. We will now use a MySQL client (I am using MySQL Workbench) to connect to the database in the virtual machine.

Homestead MySQL Connect

When you click on Test Connection, you will get the following dialog window

Homestead Connect to MySQL

If the connection is successful, you will get the following confirmation box

Homestead MySQL connection successful

Click on OK button when done

Once you are logged in, you should be able to see the following databases

MySQL Homestead Databases

Note: helloworld database has been created for us.

Letís now modify the database settings for helloworld.app

Open ~/Code/helloworld.app on the host machine.

Update the database settings to the following

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=33060
DB_DATABASE=hellloworld
DB_USERNAME=homestead
DB_PASSWORD=secret

Get back to the terminal and browse to the project root directory

Run the following command to migrate the users tables migration files

php artisan migrate

you will get the following error

SQLSTATE[HY000] [2002] Connection refused

The above error is usually caused by the DB_HOST being set to the local IP address 127.0.0.1

Change the setting to the following

DB_HOST=localhost

Save the changes

Letís now the migrate command again

This time, the tables will be created successfully. If you check the database from MySQL Workbench or whatever client app that you are using, you will be able to see the tables.

Letís now user tinker to add a user record

Run the following command to start tinker

php artisan tinker

Run the following commands to create a new user

$user = new App\User
$user->name = "Rodrick"
$user->email = "a-team@kode-blog.com"
$user->password = bcrypt('hecate')
$user->save()

Run the following query in MySQL against the helloworld database in the homestead connection ? SELECT * FROM helloworld.users;

You will get the following results

Homestead Database Users

Summary

In this tutorial, we learnt how to create a new Laravel project in homestead and working with the source files on the host machine.

Whatís next?

The next tutorial will look at how to configure homestead and other cool things that you can mess with.

Kode Blog Tutorials is dedicated to bring you update to date, high quality free tutorials. You can support us by using the social media buttons to like and share the tutorial and subscribing to our newsletter. Please use the comments section below to give us feedback.

You can sign up for a free account on our site and you will be able to keep track of your progress as you go through the tutorial series.