Simple Laravel Setup for Windows

alchermd

John Alcher

Posted on November 27, 2018

Simple Laravel Setup for Windows

Originally posted at my website

Note: I'm recently trying to get back into blogging after setting up a custom blogging platform from scratch. The content of this article isn't ground breaking, but it might be of use for some of our members. Cheers!


Introduction

Greetings everyone. In this article, I'll be showing you a simple way to setup a Laravel development environment on a Windows machine. Note that this is just one of many ways of setting up a dev workflow, but I'm focusing on simplicity and try to get away with as little complexity within our setup. I know that there's Docker, Vagrant, Homestead, and other robust solutions for this problem, but let's just stick with the basics and get started ASAP!

Target Audience

This article is for you if you are:

  • a Wordpress developer that already uses XAMPP and would like to try out a modern MVC framework like Laravel
  • experienced with other backend languages and would want to see what Laravel has to offer
  • a Linux user (like me!) that just can't figure out how to use a Windows machine for dev work
  • someone who just want to try out PHP, this setup works for you too!

What you'll need

Before we proceed, make sure to download these software onto your machine:

  • XAMPP - a prepackaged, cross-platform solution that includes multiple software for web development. It has a lot of goodies included, but we're mostly interested with PHP and MySQL
  • Composer - a dependency manager for PHP. No more manually downloading a .zip file, packages are now just a command away!
  • Visual Studio Code - a text editor from Microsoft. This is optional, you can use whatever editor you want.

The Process

Now that we got the prerequisites out of the way, let's start setting up our dev environment.

Step #1 - Start XAMPP

Make sure that MySQL has started, or else we won't be able to execute commands to our database.

Step #2 - Create a new Laravel project using Composer

Open up your command prompt (type in "cmd" without the quotes on your Start Menu) and type in the following command:

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

This command will generate a new Laravel project within a new directory named my-laravel-project. Let's make sure to move our command prompt into that directory before proceeding

> cd my-laravel-project
> dir
<DIR>  app
<DIR>  bootstrap
<DIR>  config
...
Enter fullscreen mode Exit fullscreen mode

Step #2.5 - Fix the MySQL Windows error

This is a quick gotcha. The version of MySQL provided by XAMPP has some issues detailed in this post. To fix this, open the app/Providers/AppServiceProvider.php file and replace the contents with the following snippet:

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     */
    public function boot()
    {
        Schema::defaultStringLength(191);
    }

    /**
     * Register any application services.
     */
    public function register()
    {
        //
    }
}
Enter fullscreen mode Exit fullscreen mode

Step #3 - Configure the Environment Variables

Laravel uses environment variables to hold environment specific details such as database credentials. These are found in the .env file. Let's configure the environment variables used for connecting to our database. To do this, open the .env file with your favorite text editor.

Note: if you're setting up an existing Laravel project, chances are there are no .env file yet in your directory. You can create your own .env file and copy the contents of the .env.example file as a starting point.

If you have Visual Studio Code installed, you can open your Laravel project and edit the .env file there:

> code .
Enter fullscreen mode Exit fullscreen mode

Once you opened the .env file, replace the following values:

DB_CONNECTION=mysql
DB_DATABASE=my-laravel-project
DB_USERNAME=root
DB_PASSWORD=
Enter fullscreen mode Exit fullscreen mode

Note: the default username for MySQL is root and it doesn't have a password. If you already set it to a different credential, adjust the values accordingly.

Step #4 - Set the Application Key

The Application Key, or APP_KEY in your .env file, is used by Laravel for security. Most notable is its usage in the Cross-Site Request Forgery (CSRF) protection, so it's a good idea for us to set this application key once we have our project installed. To do this, we can use an Artisan command to generate a key for us:

> C:\xampp\php\php.exe artisan key:generate
Application key set!
Enter fullscreen mode Exit fullscreen mode

Note: C:\xampp\php\php.exe is the default PHP installation path for XAMPP. Adjust this if you installed it in a different directory.

Step #5 - Run the Database Migrations

Laravel uses database migrations to define the database schema of an application. But before we do this, we need to make a MySQL database first.:

> C:\xampp\mysql\bin\mysql.exe -u root
>>> CREATE DATABASE my-laravel-project;
>>> exit;
Enter fullscreen mode Exit fullscreen mode

To make sure we can connect to our database, let's run the predefined migrations with the following command:

> C:\xampp\php\php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table
Enter fullscreen mode Exit fullscreen mode

If you had a similar output, congrats! Your Laravel app can now connect to a MySQL database.

Step #6 - Running the Application

Let's see the payoff for our efforts and run our application! To do this, use the serve artisan command in your terminal:

> C:\xampp\php\php.exe artisan serve
Laravel development server started: <http://127.0.0.1:8000>
Enter fullscreen mode Exit fullscreen mode

Open up a web browser and visit http://127.0.0.1:8000 and, if things went well, you should be greeted with the Laravel starting page

Conclusion

That's it! You now have a working Laravel setup with MySQL configured. You can now start working on a Laravel project. To learn more about the Laravel framework, you can consult the official Laravel documentation or watch some of the tutorials from Laracasts. The Laravel ecosystem is thriving and there should be ample support for whatever applications you want to build!

💖 💪 🙅 🚩
alchermd
John Alcher

Posted on November 27, 2018

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related