laravel 5.4自定义命令不起作用

时间:2017-06-13 06:33:33

标签: php cron laravel-5.4 add-custom-command

这是我第一次在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";
                }

    }

Kernel.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的\元器件\调试\异常\ FatalErrorException]   未找到“App \ Console \ Commands \ ShipNow”类

1 个答案:

答案 0 :(得分:0)

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

public static function ShipNow()
{
...