Issue with Importing Excel Data (Collection rows) to Laravel 9 Database using maatwebsite/excel 3.1

352 views Asked by At

I am trying to import multiple rows from an excel sheet using Maatwebsite-excel 3.1. Here, is the challenge, when I do. dd($row) it prints all the rows. However, the moment I am not running the code without DD, it magically "empties" the rows. Below is my code

    public function collection(Collection $rows)
    {
        $rows->shift();

        $total_amount = 0;
        foreach ($rows as $row){

            if($row->filter()->isNotEmpty()){
                $article_title =collect(trim($row[0]))[0];
                $tag = collect(trim($row[1]))[0];
                $words = collect((int) $row[2])[0];
                $deadline = collect(trim($row[3]))[0];
                $pkw = collect(trim($row[4]))[0];
                $skw1 = collect(trim($row[5]))[0];
                $skw2 = collect(trim($row[6]))[0];
                $instructions = collect(trim($row[7]))[0];
                $my_tag = UserTag::where('name', 'LIKE', '%'.$tag.'%')->first();
                $my_urgency = Urgency::where('name', 'LIKE', '%'.$deadline.'%')->first();
//                dd($tag, $words, $my_urgency);
                $amount  = $my_tag->amount_per_word * $my_urgency->amount * $words;
                $project_deadline = now()->addHours($my_urgency->hours)->format('Y-m-d H:i:s');
                Project::create([
                    'user_id'=>request()->user()->id,
                    'article_title'=>$article_title,
                    'upload_batch_id'=>$this->batch->id,
                    'user_tag_id'=>$my_tag->id,
                    'urgency_id'=>$my_urgency->id,
                    'price_per_word'=>$my_tag->amount_per_word,
                    'no_of_words'=>$words,
                    'deadline'=>$project_deadline,
                    'amount'=>($amount),
                    'primary_keyword'=>$pkw,
                    'secondary_keyword_1'=>$skw1,
                    'secondary_keyword_2'=>$skw2,
                    'instructions' => $instructions
                ]);
                $total_amount = $total_amount + $amount;
            }
        }
        $this->batch->amount = $total_amount;
        $this->batch->update();
    }

I have also used tried it without collect(trim(row[value])) but this also did not work.

I have tried following these solutions, but none of them worked for me

Here is the error enter image description here

and below is my dd statement

enter image description here

Please help me kindly.

1

There are 1 answers

0
Timothy Mach On

Got it. Implement WithHeadingRow to class then remove the $rows->shift();

This will make the first row array keys. Then you can access them later as:

class ProjectsImport implements ToCollection, WithHeadingRow
{
   public function collection(Collection $rows){
        $total_amount = 0;
        foreach ($rows as $row){

            if($row->filter()->isNotEmpty()){

                $article_title = $row['article_name'];
    }
  }
 }
}