Skip to content

Use with laravel tinker? #571

Open
Open

Description

@freestyledork

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.

Activity

  1. itsgoingd commented on Apr 7, 2022

    @itsgoingd
    Owner

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

  2. freestyledork commented on Apr 7, 2022

    @freestyledork
    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!

  3. tminich commented on Jun 2, 2022

    @tminich
    Contributor

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

  4. fgilio commented on Sep 19, 2022

    @fgilio
    Contributor

    Would be useful to also have it working with Tinkerwell

  5. added this to the Clockwork 6 milestone on Oct 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions