This is the first time i’m working in Laravel. I’m developing a custom command which will read a file and insert rows in database. I’m getting errors. Below is my code any help will be appreciated.
Command File.
namespace AppConsoleCommands; use IlluminateConsoleCommand; use IlluminateDatabaseEloquentModel; use AppShip; //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(); } }
Model File
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 = [ CommandsQuizStart::class, CommandsSendWelcomeEmailCommand::class, Commandsshipdata::class, ];
ERRORs: D:xampphtdocslara12>php artisan ship:start PHP Fatal error: Class ‘AppConsoleCommandsShipNow’ not found in D:xampphtdocslara12appConsoleCommandsshipdata.php on line 26
[SymfonyComponentDebugExceptionFatalErrorException] Class ‘AppConsoleCommandsShipNow’ not found
Advertisement
Answer
ShipNow::ShipNow()
should be Ship::ShipNow()
, since that’s the name of the Model and you call it as a static method, so it has to be a static method in the Ship file:
public static function ShipNow() { ...