The very first thing you will need to work with Laravel (and then Eloquent) is Composer. Composer is a dependency management tool for PHP. With this tool, you can easily include every dependency that is needed in your project. This is done in seconds, using a JSON configuration file namedcomposer.json.

Usually, dependencies in a PHP project were managed with PEAR or other methods. Composer has a different policy: everything works on a per-project basis. This means that you can have two projects on the same server with different versions of the same dependency package.

Installing Composer

The installation procedure is ridiculously easy. All you have to do, is to go on the Download page of the Composer website and find the right method for your operating system.

  • If you have Linux or Mac, just use this:
    curl -sS | php

    Or, if you don’t have cURL, then use this:

    php -r "readfile('');" | php

    Also, the composer.phar file will be downloaded in your current directory.

  • On Windows, you can simply download the dedicated installer.

Once Composer is installed, I suggest putting its path in the PATH variable of your system, in order to use it wherever you want. There are many ways to do it, which depend on your operating system. Let’s look at each.

  • On Linux, you can move Composer to the right directory simply with the following command:
    mv composer.phar /usr/local/bin/composer
  • The same goes for OS X, but sometimes, the usr directory doesn’t exist. You must createusr/local/bin manually.
  • Finally, on Windows, you must open the control panel and type environment variable or something similar. The search utility will do the rest for you. Once in the right window, you will get a list of all environment variables. Find PATH and add the composer installation path to it.

The composer.json and autoload files

Before we go deep into our project, let’s take a look at how Composer works.

In the composer.json file, the developer specifies every single dependency for its project. You can also create your packages, but we are not going to look at how to create them in this book.

So, let’s say that you want to create a project that uses Monolog for logging purposes.

  1. Create a folder for the project, then create an empty text file, and name it composer.json.
  2. Open it and all you will have to do is to include your dependency as shown:
          "require": {
              "monolog/monolog": "1.12.0"
  3. After that, save the file and type the following in your project directory:
    composer update

Wait a minute to download everything, and then you are done!

What? OK, here is how it works: Composer downloads every package you may need and automatically creates a loader for all your packages. So, to use your dependencies in your project, you will just need to include vendor/autoload.php, and you are good to go.

Let’s say that you have an index.php file as a start file for your application. You will have to perform something like the following:

  <?php // index.php file


    // your code here...

Nothing more!

Why am I showing this to you? Well, Laravel and Eloquent are Composer packages. So, in order to use it and create a Laravel application, you have to know how the mechanism works!

The most used commands

Composer is a command-line tool. Every good CLI tool has some important commands, and in this little section, I will show you what we are going to use the most.

  • First of all, we have the following:
    composer create-project

    With this command, you can create a new project using a specific package as a base. You will use this command to create a new Laravel project using the following syntax:

    composer create-project laravel/laravel my_project_folder
  • Then, you can find:
    composer install
    composer update

    These are two similar commands; they are similar, but not the same. When you specify your dependencies in the composer.json file, you can use install to install them. If you already installed them but you want to update your dependencies to a newer version, use update.


    In order to know what must and must not be updated, Composer uses thecomposer.lock file, which you can see in the root of your project. Actually, you will never have to work with it, but it’s important to know that Composer uses it as a log of what it does.

  • Sometimes, you will also see this:
    composer require

    You can use require to include dependencies in your project on the fly. Here’s an example of Monolog inclusion using require:

    composer.phar require monolog/monolog:1.12.0
  • Another often used command is:
    composer dump-autoload

    This command regenerates the autoload.php file. It can be useful if you add some classes into your projects without using namespaces or PSR conventions and rules.

  • Sometimes, you will have to use (after a warning):
    composer self-update

    This command updates Composer itself. Just a few seconds, and you are up and running again!

  • Finally, you can use the following special command:
    composer global COMMAND_HERE

    Use it to execute a specific command in the Composer home directory. As I mentioned before, Composer works on a per-project basis, but sometimes you will need to install some tools globally. With the global command, you can do it easily.

That’s all you need to know about Composer right now, and yes, there are many other commands, but we don’t need them now.