Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use with laravel tinker? #571

Open
freestyledork opened this issue Apr 7, 2022 · 4 comments
Open

Use with laravel tinker? #571

freestyledork opened this issue Apr 7, 2022 · 4 comments
Milestone

Comments

@freestyledork
Copy link

Is it possible to log the queries when testing with laravel tinker? If so, can you provide instructions on how to enable it? I reviewed the docs and searched the issues, I don't see any other mention of using Tinker.

@itsgoingd
Copy link
Owner

I don't think so, unless it works with collecting artisan commands? Have you tried turning it on?

@freestyledork
Copy link
Author

I tried messing with the settings to see if that was the reason it wasn't working. I can confirm it doesn't log anything when using tinker with artisan commands enabled. I'd like to suggest it as a feature. Love the package anyway!

@tminich
Copy link
Contributor

tminich commented Jun 2, 2022

I had a similar need. I solved it with a custom Psy-Shell command:

use Psy\Command\ReflectingCommand;
use Psy\VarDumper\Presenter;
use Psy\VarDumper\PresenterAware;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class MonitorDb extends ReflectingCommand implements PresenterAware
{
    private ?Presenter $presenter = null;

    /**
     * PresenterAware interface.
     *
     * @param Presenter $presenter
     */
    public function setPresenter(Presenter $presenter): void
    {
        $this->presenter = $presenter;
    }

    /**
     * {@inheritdoc}
     */
    protected function configure(): void
    {
        $this
            ->setName('monitor_db')
            ->setAliases(['mdb', 'debug_db', 'ddb'])
            ->setDescription('Start monitoring database queries');
    }

    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        \DB::listen(function ($query) use ($output){
            $data = [
                'sql' => $query->sql,
                'bindings' => $query->bindings,
                'execution_time' => $query->time,
            ];

            $output->writeln($this->presenter->present($data));
        });
        return 0;
    }

}

You need to load the command in your config.tinker.php (which you need to enable in you .env).

@fgilio
Copy link
Contributor

fgilio commented Sep 19, 2022

Would be useful to also have it working with Tinkerwell

@itsgoingd itsgoingd added this to the Clockwork 6 milestone Oct 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants