【问题标题】:laravel 5.4 custom command not workinglaravel 5.4 自定义命令不起作用
【发布时间】:2022-03-03 23:05:09
【问题描述】:

这是我第一次在 Laravel 工作。我正在开发一个自定义命令,它将读取文件并在数据库中插入行。我收到错误。以下是我的代码,任何帮助将不胜感激。

命令文件。

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Model;

use App\Ship;
//use DB;

class shipdata extends Command
{
    protected $signature = 'ship:start';

    protected $description = 'This Command will Read file and Extract data from it and will store data in database';

    public function __construct()
    {
        parent::__construct();

    }

    public function handle()
    {
//            $this->line('Query Working');
        ShipNow::ShipNow();
    }
}

模型文件

    public function ShipNow()
    {

            $dir = "d:/xampp/htdocs/lara12/IN/";
            $failed_dir = "d:/xampp/htdocs/lara12/FAILED/";
            $processed_dir = "d:/xampp/htdocs/lara12/PROCESSED/";
            $files = array();

            // Get all files which need to process
                foreach (glob($dir."*.SHIP") as $file) {
                $files[] = $file;
                }

            // If .SHIP files available in the folder then start processing
        if(count($files) > 0)
        {

            // Start processing file
            foreach($files as $file)
            {
                $row = 1;
                $file_name = $file;

                // Get file name without extension
                $withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file_name);

                // Generate new filename with extension PROC
                $rename_filename = $withoutExt.".PROC";

                // Rename filename
                $file_proc = rename($file_name, $rename_filename);

                // File processing
                $error = 0;
                if (($handle = fopen($rename_filename, "r")) !== FALSE) {
                    while (($data = fgetcsv($handle, 1000, "|")) !== FALSE) {

                        $num = count($data);
                        // For header table insert
                        if($row==1)
                        {
                            $myparams['warehouse'] = $data[0];
                            $myparams['SHIPMENT_ID'] = $data[1];
                            $myparams['COMPANY'] = $data[2];
                            $myparams['CARRIER'] = $data[3];
                            $myparams['CARRIER_SERVICE'] = $data[4];
                            $myparams['CUSTOMER'] = $data[5];
                            $myparams['CUSTOMER_NAME'] = $data[6];
                            $myparams['SHIP_TO_NAME'] = $data[7];
                            $myparams['SHIP_TO_ADDRESS1'] = $data[8];
                            $myparams['SHIP_TO_ADDRESS2'] = $data[9];
                            $myparams['SHIP_TO_ADDRESS3'] = $data[10];
                            $myparams['SHIP_TO_CITY'] = $data[11];
                            $myparams['SHIP_TO_COUNTRY'] = $data[12];
                            $myparams['SHIP_TO_POSTAL_CODE'] = $data[13];
                            $myparams['SHIP_TO_ATTENTION_TO'] = $data[14];
                            $myparams['SHIP_TO_PHONE_NUM'] = $data[15];
                            $myparams['SHIP_TO_EMAIL_ADDRESS'] = $data[16];
                            $myparams = save();

                           /* $sql = "exec dbo.Portal_3PL_ShipmentEntry_Header '".$myparams['SHIPMENT_ID']."','".$myparams['COMPANY']."','".$myparams['CARRIER']."','".$myparams['CARRIER_SERVICE']."','".$myparams['CUSTOMER']."','".$myparams['CUSTOMER_NAME']."','".$myparams['SHIP_TO_NAME']."','".$myparams['SHIP_TO_ADDRESS1']."','".$myparams['SHIP_TO_ADDRESS2']."','".$myparams['SHIP_TO_ADDRESS3']."','".$myparams['SHIP_TO_CITY']."','".$myparams['SHIP_TO_COUNTRY']."','".$myparams['SHIP_TO_POSTAL_CODE']."','".$myparams['SHIP_TO_ATTENTION_TO']."','".$myparams['SHIP_TO_PHONE_NUM']."','".$myparams['SHIP_TO_EMAIL_ADDRESS']."'";
                            $sql_result = odbc_exec($conn, $sql);
                            if (odbc_error())
                            {
                                echo odbc_errormsg($conn);
                                $error = 1;

                            }
                            $ERP_ORDER_LINE_NUM = 1;
                            $shipment_header = odbc_fetch_array($sql_result);*/
                        }
                        // For shipment detail
                        else
//                        {
//                            $ITEM = $data[1];
//                            $QUANTITY = intval($data[2]);
//                            $INTERNAL_SHIPMENT_NUM = $shipment_header['INTERNAL_SHIPMENT_NUM'];
//
//                            $query2 = "exec dbo.Portal_3PL_ShipmentEntry_Detail '".$INTERNAL_SHIPMENT_NUM."','".$myparams['COMPANY']."',".$ERP_ORDER_LINE_NUM.",'".$ITEM."','".$QUANTITY."',NULL,'N'";
//                            echo $query2.PHP_EOL;
//                            $query_result2 = odbc_exec($conn, $query2);
//                            if (odbc_error())
//                            {
//                                echo odbc_errormsg($conn);
//                                $error = 1;
//
//                            }else{
//                                $ERP_ORDER_LINE_NUM++;
//                            }
//                            $shipment_detail = odbc_fetch_array($query_result2);
//
//                            echo trim($shipment_detail['Result'])."@".trim($shipment_detail['Comment'])."@".trim($shipment_detail['INTERNAL_SHIPMENT_LINE_NUM']);
//                        }
                        $row++;
                    }


                    if($error == 1)
                    {
                        $failed_file = "FAILED/".basename($withoutExt).".FAIL";
                        $file_to_move = "IN/".basename($rename_filename);

                        fclose($handle);
                        rename($file_to_move, $failed_file);

//                        $sql = "INSERT INTO qcimportlog (filename, recordprocess, status) VALUES
//                                                                        ('$file_name', '$row', 'failed')";

//                        $result = mysql_query($sql) or die(mysql_error());

                    }
                    else
                    {
                        $processed_file = "PROCESSED/".basename($rename_filename);
                        $file_to_move = "IN/".basename($rename_filename);
                        echo $processed_file;

                        fclose($handle);
                        rename($file_to_move, $processed_file);

//                        $sql = "INSERT INTO qcimportlog (filename, recordprocess, status) VALUES
//                                                                        ('$file_name', '$row', 'success')";

//                        $result = mysql_query($sql) or die(mysql_error());
                    }
                }

            }
        }
                else
                {
                    echo "No files to process";
                }

    }

内核.php

    protected $commands = [
         Commands\QuizStart::class,
         Commands\SendWelcomeEmailCommand::class,
        Commands\shipdata::class,

    ];

错误: D:\xampp\htdocs\lara12>php artisan ship:start PHP 致命错误:第 26 行的 D:\xampp\htdocs\lara12\app\Console\Commands\shipdata.php 中找不到类 'App\Console\Commands\ShipNow'

[Symfony\Component\Debug\Exception\FatalErrorException] 找不到类“App\Console\Commands\ShipNow”

【问题讨论】:

  • 你得到什么错误?
  • D:\xampp\htdocs\lara12>php artisan ship:start PHP 致命错误:在 D:\xampp\htdocs\lara12\app\ 中找不到类 'App\Console\Commands\ShipNow'第 26 行的 Console\Commands\shipdata.php [Symfony\Component\Debug\Exception\FatalErrorException] 找不到类“App\Console\Commands\ShipNow”
  • 你必须导入 ShipNow 类而不是 Ship。
  • @JuhG ShipNow 是函数名,模型的类名是 Ship
  • @rameez.hashmi 那么它可能是一个错字:ShipNow::ShipNow(); 应该是 Ship::ShipNow();

标签: php laravel-5 cron


【解决方案1】:

ShipNow::ShipNow() 应该是Ship::ShipNow(),因为这是模型的名称,并且您将其称为静态方法,因此它必须是 Ship 文件中的静态方法:

public static function ShipNow()
{
...

【讨论】:

    猜你喜欢
    • 2017-07-20
    • 2017-10-10
    • 2019-06-21
    • 1970-01-01
    • 2020-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-09
    相关资源
    最近更新 更多