Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Mysql Cli Client Laravel Package

syamsoul/laravel-mysql-cli-client

View on GitHub
Deep Wiki
Context7

Getting Started

  1. Installation:

    composer require syamsoul/laravel-mysql-cli-client
    

    Verify the package is autoloaded by running composer dump-autoload.

  2. First Use Case: Run the Artisan command to access MySQL CLI directly:

    php artisan db:access
    
    • Where to look first: Check the .env file for DB_* credentials (e.g., DB_HOST, DB_DATABASE, DB_USERNAME, DB_PASSWORD). The package uses these by default.
    • Expected behavior: The command should spawn a MySQL CLI session using the Laravel database configuration.

Implementation Patterns

Basic Workflow

  1. 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.)

  2. 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)
        }
    }
    
  3. 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.)

  4. 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);
    }
    

Gotchas and Tips

Pitfalls

  1. Package Maturity:

    • The package is marked as "NOT WORKING YET" and "Still In Development." Test thoroughly in a staging environment before relying on it in production.
    • Debugging: If the command fails silently, check:
      • MySQL server accessibility from the Laravel host.
      • Correct .env credentials (e.g., DB_PASSWORD may require quotes if it contains special characters).
      • Laravel’s storage/logs/laravel.log for errors.
  2. Missing Features:

    • No Config Publishing: The package may not support customizing connection parameters (e.g., port, socket) via config files. Hardcode overrides in .env if needed:
      DB_SOCKET=/tmp/mysql.sock  # Example for Unix socket connections
      
    • Limited Command Options: The db:access command may lack flags for advanced use cases (e.g., --database, --user). Extend the command class if needed.
  3. Security:

    • Avoid exposing the db:access command in shared environments (e.g., production servers) where unauthorized CLI access could be a risk.
    • Use Laravel’s Artisan gates to restrict access:
      // In AppServiceProvider
      Gate::define('access-db-cli', function ($user) {
          return $user->isAdmin(); // Example condition
      });
      

Tips

  1. 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.

  2. 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'
    
  3. 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');
        }
    }
    
  4. Logging Queries: Pipe output to a log file for auditing:

    php artisan db:access > query_log.sql
    
  5. 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)
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
nasirkhan/laravel-sharekit
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony