millzbillz

millzbillz

Software developer at NextByte

Member Since 9 Months Ago

Dar es Salaam

Experience Points
50
Total
Experience

4,950 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed
0
Lessons
Completed
Best Reply Awards
0
Best Reply
Awards
  • start-engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-session Created with Sketch.

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • subscriber-token Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer-token Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • lara-evanghelist Created with Sketch.

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • chatty-cathy Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

Level 1
50 XP
Jul
12
4 months ago
Activity icon

Started a new Conversation Maatwebsite 3.1 On Importing Queue Failes

Below is error on a laravel.log

vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php  
[2019-07-11 14:48:47] development.ERROR: [0] File "/tmp/laravel-excel 4noteGu1gFjJoFClKJQsLw8SgDShm1nd.xlsx" does not exist. on line 344 of file vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php  
[2019-07-11 14:48:47] development.ERROR: [0] File "/tmp/laravel-excel-4noteGu1gFjJoFClKJQsLw8SgDShm1nd.xlsx" does not exist. on line 344 of file vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php 

Here is a queue error

[2019-07-11 14:48:47][287] Processing: Maatwebsite\Excel\Jobs\QueueImport
[2019-07-11 14:48:47][287] Processed:  Maatwebsite\Excel\Jobs\QueueImport
[2019-07-11 14:48:47][288] Processing: Maatwebsite\Excel\Jobs\ReadChunk
[2019-07-11 14:48:47][289] Processing: Maatwebsite\Excel\Jobs\ReadChunk
[2019-07-11 14:48:47][290] Processing: Maatwebsite\Excel\Jobs\ReadChunk
[2019-07-11 14:48:47][290] Failed:     Maatwebsite\Excel\Jobs\ReadChunk

Here is my controller function

public function store(Request $request)
{
     Excel::import(new HsCodeImport(),"650.xlsx",'local'); 
     return redirect()->back()->withFlashSuccess(__('label.app_success'));
}

Here is my Import file on App\Imports\HsCodeImport.php

use App\Exceptions\GeneralException;
use App\Models\Application\Hscode;
use App\Models\ReceiptCode\ReceiptCode;
use Carbon\Carbon;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithBatchInserts;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;

class HsCodeImport implements ToCollection, WithHeadingRow, ShouldQueue, WithChunkReading
{
    use Importable;

    protected $receiptCode;
    protected $chunk = 500;
    protected $hscode_key = 'hscode';
    protected $description_key = 'description';
    protected $regulatory_status_key = 'regulatory_status';
    protected $comment_key = 'comment';
    protected $headings = ['hscode','description','regulatory_status','comment'];

    public function __construct()
    {

    }

    /**
     * @param Collection $collection
     * @throws GeneralException
     */
    public function collection(Collection $collection)
    {
        /*fetching the first Collection*/
        $columns = $collection->first();

        if (!$columns->has($this->headings)) {
            /*When file has different headings*/

        } else {
            /*When the file has expected headings*/

            /*Truncate temp table*/
            DB::table('hs_code_temps')->truncate();

            /*Counting rows on a file*/
            $original_file_rows_count = $collection->count();


            /*Chunk the file data according @var $chunk*/
            $chunks = $collection->chunk($this->chunk);

            /*read each chunks insert into Temporary table and validate to get if there is error in each row*/
            $chunks->each(function ($item) {
                /*Iterate on each chunk*/
                $item->toArray();

                foreach ($item as $row) {

                    /* start: Validating row entries */
                    $error_report = "";
                    $error = 0;

                    foreach ($row as $key => $value) {
                        if (trim($key) == $this->hscode_key) {
                            if (trim($value) == "" Or $value == NULL) {
                                $error = 1;
                                $error_report = $error_report . trans('exceptions.backend.upload.entries', ['column' => $key, 'entry' => $value]) . ", \r\n";
                                $row[$key] = NULL;
                            }
                        } elseif (trim($key) == $this->description_key) {
                            if (trim($value) == "" Or $value == NULL) {
                                $error = 1;
                                $error_report = $error_report . trans('exceptions.backend.upload.entries', ['column' => $key, 'entry' => $value]) . ", \r\n";
                                $row[$key] = NULL;
                            }
                        } elseif (trim($key) == $this->regulatory_status_key) {
                            if (trim($value) == "" Or $value == NULL) {
                                $error = 1;
                                $error_report = $error_report . trans('exceptions.backend.upload.entries', ['column' => $key, 'entry' => $value]) . ", \r\n";
                                $row[$key] = NULL;
                            }
                        }

                    }

                    /*Inserting into Temp table*/
                    DB::table('hs_code_temps')->insert([
                        'code' => $row[$this->hscode_key],
                        $this->description_key => $row[$this->description_key],
                        $this->regulatory_status_key => $row[$this->regulatory_status_key],
                        $this->comment_key => $row[$this->comment_key],
                        'receipt_code_id' => 1/*$receiptCode->id*/,
                        'error' => $error,
                        'error_report' => $error_report,
                        'created_at' => Carbon::now(),
                        ]);

                    /* end: Validating row entries*/

                }

            });

            /*compare total rows with no error to total rows of the file*/
            $total_temp_rows_count = DB::table('hs_code_temps')->whereError(0)->count();

            if ($total_temp_rows_count != $original_file_rows_count) {
                /*When there is error and rows are not equal*/
            } else {
                /*When there is no error*/
                $originalHsCode = new Hscode();
                $temp_table = DB::table('hs_code_temps')->get(['code', $this->description_key, $this->regulatory_status_key, $this->comment_key])/*->toArray()*/;
                /*Iterate throw the rows in a temp row*/
                foreach ($temp_table as $object) {
                    /*copy data from temp table to origin table*/
                    $originalHsCode->create(get_object_vars($object));
                }
                /*Truncate temp table*/
                DB::table('hs_code_temps')->truncate();

            }


        }


    }

    public function headings(): array
    {
        return ['HSCode','Description','Regulatory_Status','Comment'];
    }


    public function chunkSize(): int
    {
        return 500;
    }

    public function batchSize(): int
    {
        return 500;
    }

}