Setting up a New Project Database in Homestead with Envoy

If you’ve just started using Laravel Homestead you know just how easy it makes getting started on a new project. On my first attempt I was up and running in under five minutes…very cool!

While adding a new project today I realized there isn’t a built-in way to do project-specific provisioning. There are a few tasks I do on nearly every project and I’d prefer to not have to jump into the VM to do them (mostly because I have to always look up the commands). Enter Envoy.

Nearly every site has a database. Why not automate its creation? Envoy can take care of that in a snap.

MySQL

@servers(['vm' => '-p 2222 vagrant@127.0.0.1'])

@task('init-local', ['on' => 'vm'])
    mysql -u homestead -psecret -e "create database :db_name
@endtask

Postgres

@servers(['vm' => '-p 2222 vagrant@127.0.0.1'])

@task('init-local', ['on' => 'vm'])
    echo "*:*:*:*:secret" > ~/.pgpass
    chmod 600 ~/.pgpass
    createdb -h 127.0.0.1 -U homestead -O homestead :db_name
    rm ~/.pgpass
@endtask

This is just the beginning of what you could do for a new project within Homestead. Other obvious tasks might be migrating and seeding the database, running custom Artisan commands, or even other sync’d shell scripts. It’s a breeze!

  • Shame Envoy doesn’t work on Windows.

  • You could modify the @servers line to ‘vagrant@127.0.0.1’ and then run the envoy command within your VM.

  • So @garrettstjohn:disqus – I can’t seem to get Envoy to connect. I have an Envoy.blade.php file in the root of my project and I have the following:

    https://gist.github.com/tannerhearne/8a38a996629e1a1e7b54

    Any ideas?

    Thanks,
    Tanner

  • Are you getting any errors? Also, make sure your @task line is updated to reference the correct name (‘homestead’ instead of ‘vm’):

    @task(‘init-local’, [‘on’ => ‘homestead’])

  • I need to check for server errors. I actually set it up how you have it, but couldn’t get it to work, so I Googled “homestead and envoy laravel” and your blog was the second result. :) So no luck yet – let me dive deeper to see if I find errors – it is acting like it isn’t even connecting to the server though.