Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An artisan command for opening the project's database in TablePlus
<?php
Artisan::command('db:open {connection?}', function ($connection = null) {
if (! file_exists('/Applications/TablePlus.app')) {
$this->warn('This command uses TablePlus, are you sure it\'s installed?');
$this->line("Install here: https://tableplus.com/\n");
}
$driver = $connection ?: config('database.default');
$host = config("database.connections.{$driver}.host");
$user = config("database.connections.{$driver}.username");
$password = config("database.connections.{$driver}.password");
$database = config("database.connections.{$driver}.database");
if ($driver === 'sqlite') {
exec("open {$database}");
} else {
exec("open {$driver}://{$user}:{$password}@{$host}/{$database}");
}
});
@godkinmo

This comment has been minimized.

Copy link

@godkinmo godkinmo commented Feb 3, 2020

You inspire me, I convert your script to bash script as function in my ~/.zshrc

function dbopen() {
  if [ -d '/Applications/TablePlus.app' ]; then
    TP_DRIVER=$(grep -m1 DB_CONNECTION .env | cut -d '=' -f2)
    TP_HOST=$(grep -m1 DB_HOST .env | cut -d '=' -f2)
    TP_USER=$(grep -m1 DB_USERNAME .env | cut -d '=' -f2)
    TP_PASSWORD=$(grep -m1 DB_PASSWORD .env | cut -d '=' -f2)
    TP_DATABASE=$(grep -m1 DB_DATABASE .env | cut -d '=' -f2)

    if [ $DRIVER = "sqlite" ]; then
      open $TP_DATABASE
    else
      open $TP_DRIVER://$TP_USRE:$TP_PASSWORD@$TP_HOST/$TP_DATABASE
    fi
  else
    echo "This command uses TablePlus, are you sure it's installed?"
    echo "Install here: https://tableplus.com/"
  fi
}
@jimbojsb

This comment has been minimized.

Copy link

@jimbojsb jimbojsb commented Feb 9, 2020

Does anyone know if any other options are supported? I.E. can I tell TP to use the "local" color scheme when it opens? I don't want to save a permanent connection, but it'd be cool to mimic some of the same features.

@jimbojsb

This comment has been minimized.

Copy link

@jimbojsb jimbojsb commented May 14, 2020

Apparently there are other features. I blogged about it here: Advanced TablePlus Command Line Usage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment