Custom Laravel Database Connection
Wednesday, April 18, 2018

While working with the amazing package Tenancy which provides multi-tenancy support to Laravel, I ran into a problem with using Laravel Passport. The package works by setting up an individual database for each tenant that your application has. It makes this possible by overwriting the $connection property of Eloquent models.

Unfortunately, this task can prove to be difficult for third-party packages where you cannot extend the Eloquent models. This is very much the case for Laravel Passport.

Luckily, we have access to the TokenRepository class used by Laravel. An instance of this repository is bound to Laravel's IOC. This means that we have the freedom to overwrite this repository using Laravel's bind method on the container.

$this->app->bind(\Laravel\Passport\TokenRepository::class, \App\Support\Oauth\TokenRepository::class);

We are now free to overwrite any methods in the TokenRepository, including changing the database connection for the application and thus for Laravel Passport.


namespace App\Support\Oauth;

use Illuminate\Contracts\Foundation\Application;
use Laravel\Passport\TokenRepository as BaseTokenRepository;

class TokenRepository extends BaseTokenRepository
    public function __construct(Application $app)
        $app['config']->set('database.default', $app['config']->get('tenancy.db.tenant-connection-name'));

In theory, the database connection should only be changed for Laravel Passport when calling any of its controllers that use this TokenRepository, making it an ideal place to hook into. Any direct access of the Eloquent models in Laravel passport may always be extended when needed for your own purposes.

Overiding the connection for any third-party Eloquent models is also possible by using an observer and hooking into the events that models emit. You see an example at cc08069c639af9db686412f576163f268a4a8202.

Laravel Forge Upgrade to PHP 7.1
Sunday, February 05, 2017

Laravel Forge is a lovely service for not having to worry about your server deployment process. It really saves me a lot of time. However, I wish that the creators would support upgrading PHP versions for existing servers.

In the past, I destroyed my server and deployed a new one to benefit from the new PHP version. Since then, I have more websites and setting everything up again sounded like a lot of work. So, I decided to try and update the PHP version myself.


First, SSH into your Laravel Forge server and make sure that you have your sudo password; you will need it.

We will add Ondřej Surý list of PHP packages for Debian so that we can download the new version of PHP.

sudo add-apt-repository ppa:ondrej/php

We will now delete our local cache of packages forcing new packages to show up.

sudo apt-get update

Download PHP 7.1 and install it.

sudo apt-get install php7.1

Since Laravel Forge runs on Nginx, we will download the PHP-FPM package and its dependencies.

sudo apt-get install php7.1-fpm

Next up, we will update our Nginx configuration. Let's open the config with your editor. For this tutorial, I am using VI.

sudo vi /etc/nginx/sites-enabled/default

You should now see a file that looks like this:

include forge-conf/default/before/*;

server {
    listen 80 default_server;
    server_name default;
    root /home/forge/default/public;

    # ssl_certificate;
    # ssl_certificate_key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    index index.html index.htm index.php;

    charset utf-8;

    include forge-conf/default/server/*;

    location / {
        try_files $uri $uri/ /index.php?$query_string;

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/default-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;

    location ~ /\.ht {
        deny all;

include forge-conf/default/after/*;

Do not worry about all of the scary looking configuration parameters.

We want to change one line only. If you are using VI, you can hit "i" on your keyboard to enter insert mode. This will allow you to type. Change the following line: fastcgi_pass unix:/var/run/php/php5.6-fpm.sock; Replace it with: fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;

If you are using VI, you can hit "ESC" and then ":". This will bring up a ":" at the bottom. Write wq to save your changes and exit.

The hard part is out of the way.

Restart your server.

sudo service nginx restart

Your websites should now all be running on PHP 7.1. Congratulations!

You can check your PHP version with the following command:

php -v

You should see something like:

PHP (cli) (built: Jan 20 2017 09:43:29) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache, Copyright (c) 1999-2017, b                                                                                                                                                             y Zend Technologies

Good job and enjoy!

Easy One Click Laravel Homestead Shortcut (Windows)
Tuesday, October 04, 2016

If you are like me and work a lot in Laravel Homestead on your Windows computer, would it not be lovely to be able to fire that virtual environment up with one button? I used to have to open my command prompt, change directory and then write vagrant up. What makes matters worse is that you have to wait for Vagrant to fire up.


We should first download and install the required programs. They will make your life as a developer easier anyways. Skip this step if you have already installed the programs below.

  1. Vagrant
  2. Laravel Homestead
  3. Git for Windows

Let's Get Started

Step 1. Create a bash file

For simplicity, we will call this file I have saved it in my user folder at C:\Users\Bryse\.

Place the following contents into your newly created

cd /c/Homestead
echo "Starting Vagrant"
vagrant up
vagrant ssh
echo "Suspending Vagrant"
vagrant suspend

This will change your active directory to whatever path after the first cd command. I have Homestead installed into a folder at the path C:\Homestead. In the bash command C:\ becomes /c/. It will then run vagrant up and vagrant ssh so that you can get started right away.

It is always recommended to suspend Vagrant when finished. If you are done with your virtual enviroment, you can type exit. This will make you logout from the SSH session and automatically suspend Vagrant for you.

Step 2. Create the shortcut

This is the tricky part. We want our shortcut to open Git Bash and then run the file that we made.

Go ahead and create a new shortcut anywhere on your computer. Set the target to the following: C:\Windows\System32\cmd.exe /c ""C:\Program Files\Git\bin\sh.exe" --login -i -- "C:\Users\Bryse\""

Please note that you will have to change the directory paths if they are different for your system.

I will break down what is going on here.

  1. C:\Windows\System32\cmd.exe will tell the shortcut to launch a new command prompt.
  2. ""C:\Program Files\Git\bin\sh.exe" --login -i" tells the command prompt to run this program.
  3. --login -i will log you in the for git bash Linux subsystem. Git bash does this automatically when opening a new prompt.
  4. -- "C:\Users\Bryse\" comes last. This will tell the new git bash prompt to run our bash script that we made earlier.

Step 3. Give the shortcut an icon

I like to keep my system looking nice, so I found a cool icon to use for my new shortcut.

You can use to convert a .png to a .ico. Right click your shortcut and go to properties. You can then set your shortcut's icon to the icon we just converted.

I have pinned my shortcuts to my Task Bar on Windows 10.

Step 4. Get coding!

With our fancy new shortcut, you can easily start Laravel Homestead with a click of a button.

Happy coding!