2

I'm using Artisan to call a Laravel 5 Route via the command line. I created a command line controller by following the directions in this post: Call laravel controller via command line.

I don't receive any errors when I navigate to this route in a web browser. This route performs an API call and inserts data into a MySQL database by using a Laravel Model I created called Ticker.

When I can call my route from the CLI, like so:

$ php artisan route:call /d17059dfce4c09ef5e437b1d9455f7c6

I'm getting a Laravel error (where truncate 'tickers' is the first SQL command executed in the controller which the route calls, and tickers is the table within the database), stating:

SQLSTATE[HY000] [2002] No such file or directory (SQL: truncate `tickers`)

Because I only seem to be getting the error when executing this route through the command line, it leads me to believe there must be some issue with the command line script App\Console\Commands\CallRoute.php which looks like this:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Http\Requests;

class CallRoute extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'route:call {uri}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'php artsian route:call /route';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $request = Request::create($this->argument('uri'), 'GET');
        $this->info(app()->make(\Illuminate\Contracts\Http\Kernel::class)->handle($request));
    }
}

Any help troubleshooting or resolving this error would be greatly appreciated. Thank you in advance.

sadmicrowave
  • 39,964
  • 34
  • 108
  • 180

4 Answers4

2

You should pass in /d17059dfce4c09ef5e437b1d9455f7c6 as an option rather than an argument. It's interpreting that as a directory.

php artisan route:call --uri=/d17059dfce4c09ef5e437b1d9455f7c6

Update the command signature to include the uri option:

protected $signature = 'route:call {--uri=}';

Command:

class CallRoute extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'route:call {--uri=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'php artsian route:call /route';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $request = Request::create($this->option('uri'), 'GET');
        $this->info(app()->make(\Illuminate\Contracts\Http\Kernel::class)->handle($request));
    }
}
Brian Lee
  • 17,904
  • 3
  • 41
  • 52
  • When I issue that command I get the following error: `The "--uri" option does not exist.` – sadmicrowave Apr 08 '18 at 03:02
  • You need to define `uri` in your command's signature. I'll update the answer. – Brian Lee Apr 08 '18 at 03:03
  • Weird, but now the error switched to: `The "uri" argument does not exist. ` – sadmicrowave Apr 08 '18 at 03:06
  • Update the `handle` method to use `$this->option('uri')` rather than `$this->argument()` – Brian Lee Apr 08 '18 at 03:07
  • Those errors about the `uri` are now resolved, but I'm back to square one with the original error stated in my OP and my database still isn't being updated – sadmicrowave Apr 08 '18 at 03:09
  • Hmm, does your db connection use`127.0.0.1` or `localhost`? If you're using a socket, check your `php.ini` and make sure the `mysql.default_socket` setting is pointing to the correct file. – Brian Lee Apr 08 '18 at 03:13
  • I have it set to `127.0.0.1` in both the `.env` and `config/database.php` files to force `PDO` to use the TCP protocol. – sadmicrowave Apr 08 '18 at 03:15
  • Does the mysql error log show the connection attempt being made with the correct settings? – Brian Lee Apr 08 '18 at 03:25
  • I don't see an entry being made at all in the error log when I run the command – sadmicrowave Apr 08 '18 at 03:34
  • Any additional ideas? I'm still having trouble understanding what is happening. I know I could create a standalone PHP script with MySQL connection, but I should probably use the Laravel framework to get the most use out of the Models and database connection, etc. – sadmicrowave Apr 10 '18 at 16:30
1

Try to take a look at this issue.

PDOException SQLSTATE[HY000] [2002] No such file or directory

I had similar issues when my php-cli had troubles connection to mysql, while my php-fpm was working fine(it could also be the reason why you don't have an error while you navigate to this route in the browser). I had to enable mysql pdo extension in my php-cli config.

Just run php -v in the console, and if you see something similar:

PHP 7.2.1 (cli) (built: Jan  8 2018 23:39:24) ( NTS )

It probably means you have to enable pdo support for php-cli

Tachi
  • 2,042
  • 3
  • 27
  • 44
  • I located my php-cli config for MAMP in `/Applications/MAMP/bin/php/php7.2.1/conf/php.ini` and added the line for `extension=pdo_mysql.so` but I'm still receiving the same error when I attempt to CLI command execution – sadmicrowave Apr 11 '18 at 01:01
  • Honestly, I don't know what I did to fix it, but it is fixed now. I tried changing several things and cleared the artisan config cache and its working now. I've started removing/un-doing the fixes I tried one-by-one and now I'm back to square one, but its still working... – sadmicrowave Apr 11 '18 at 01:20
0

Perhaps you meant to say TRUNCATE TABLE tickers? Are you throwing away everything it that table?

Rick James
  • 135,179
  • 13
  • 127
  • 222
  • I am throwing away everything in the table, but the syntax in my OP isn't the actual syntax being used; I'm using a Laravel Model to perform the operation which evaluates to what the error displays. Again, everything works fine in a web browser, just not from CLI. In any event, the issue is now resolved. – sadmicrowave Apr 11 '18 at 13:41
0

php artisan command create and object of command classes(Custom commands you have created). This is to fetch the list of available commands and options.

You might have added database query (truncate table tickers) Any of your Commands files constructor (__construct()).

Jithin Jose
  • 1,761
  • 2
  • 19
  • 35