syamsoul/laravel-mysql-cli-client
Installation:
composer require syamsoul/laravel-mysql-cli-client
Verify the package is autoloaded by running composer dump-autoload.
First Use Case: Run the Artisan command to access MySQL CLI directly:
php artisan db:access
.env file for DB_* credentials (e.g., DB_HOST, DB_DATABASE, DB_USERNAME, DB_PASSWORD). The package uses these by default.Direct CLI Access:
Use php artisan db:access for ad-hoc MySQL queries, debugging, or schema inspection without leaving your terminal.
Example:
php artisan db:access -e "SELECT * FROM users LIMIT 10;"
(Note: The -e flag may not be implemented; verify via php artisan db:access --help.)
Integration with Custom Commands: Extend the package’s functionality by creating a custom Artisan command that leverages its underlying logic:
use Syamsoul\LaravelMysqlCliClient\Commands\DbAccessCommand;
class CustomDbCommand extends Command {
protected $signature = 'db:custom-query {query}';
protected $description = 'Run a custom MySQL query via CLI';
public function handle() {
$query = $this->argument('query');
// Use the package's MySQL connection logic here
// (Check source code for `DbAccessCommand` to replicate behavior)
}
}
Environment-Specific Connections:
Override the default .env credentials for specific environments by publishing the package’s config (if supported):
php artisan vendor:publish --provider="Syamsoul\LaravelMysqlCliClient\ServiceProvider"
(Note: Config publishing may not be implemented; inspect the package for config/db-access.php.)
Automated Testing: Use the CLI access in PHPUnit tests to validate database states:
public function testDatabaseState() {
$this->artisan('db:access', [
'input' => "SELECT COUNT(*) FROM users;",
])
->expectsOutput('10') // Example assertion
->assertExitCode(0);
}
Package Maturity:
.env credentials (e.g., DB_PASSWORD may require quotes if it contains special characters).storage/logs/laravel.log for errors.Missing Features:
.env if needed:
DB_SOCKET=/tmp/mysql.sock # Example for Unix socket connections
db:access command may lack flags for advanced use cases (e.g., --database, --user). Extend the command class if needed.Security:
db:access command in shared environments (e.g., production servers) where unauthorized CLI access could be a risk.// In AppServiceProvider
Gate::define('access-db-cli', function ($user) {
return $user->isAdmin(); // Example condition
});
Alias the Command:
Add an alias to your ~/.bashrc or ~/.zshrc for convenience:
alias dbcli='php artisan db:access'
Then use dbcli directly in your terminal.
Combine with tmux or screen:
Run the CLI in a detached session for long-running queries:
tmux new -s mysql_session 'php artisan db:access'
Extend for Multi-Database Projects: If your app uses multiple databases, create a wrapper command to switch contexts:
// app/Console/Commands/SwitchDatabase.php
class SwitchDatabase extends Command {
protected $signature = 'db:switch {database}';
protected $description = 'Switch to a specific database';
public function handle() {
putenv("DB_DATABASE={$this->argument('database')}");
$this->call('db:access');
}
}
Logging Queries: Pipe output to a log file for auditing:
php artisan db:access > query_log.sql
Fallback to Raw MySQL CLI:
If the package fails, use the raw MySQL CLI with Laravel’s .env credentials:
mysql -h $(grep DB_HOST .env | cut -d'=' -f2) -u $(grep DB_USERNAME .env | cut -d'=' -f2) -p$(grep DB_PASSWORD .env | cut -d'=' -f2) $(grep DB_DATABASE .env | cut -d'=' -f2)
How can I help you explore Laravel packages today?