首頁  >  問答  >  主體

如何在透過 laravel excel 匯入 xlsx 檔案時執行自訂任務或觸發事件

我是 Laravel 新手, 我想從 xlsx 檔案將學生詳細資訊插入 mysql 資料庫。 我使用 Laravel excel v3 導入 excel 檔。它運行良好。但是,除了在 1 個表中插入學生詳細資料之外,還應在所有關聯表中建立相同的學生 ID 記錄。

範例--> 如果在「student_details」表中插入 1 個學生,則必須在「oral」和「endsem」表中建立 1 筆記錄,外鍵為「student_id」。

我已經舉辦了活動,在口頭表和最終表中記錄這些記錄。 現在的問題是如何應用該事件以及如何在創建學生以觸發事件後獲取學生 ID。 (學生ID將是auto_increment值)

StudentImport -->

#
<?php
namespace App\Imports;

use App\Events\StudentCreated;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Validators\Failure;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\SkipsOnFailure;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Concerns\SkipsFailures;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use App\Models\StudentDetails;

class StudentsImport implements ToModel, SkipsOnFailure, WithValidation, WithHeadingRow
{
    use Importable, SkipsFailures;

    /**
    * @param Collection $collection
    */
    public function model(array $row)
   {     
        return new StudentDetails([
            'roll_no' => $row['roll_no'],
            'student_id' => $row['student_id'],
            'div' => $row['div'],
            'name' => $row['name'],
            'gender' => $row['gender'],
            'user_key' => session()->get('user_id'),
            'group_key' => $group_key
        ]);
    }

    public function onFailure(Failure ...$failures)
    {
        // Handle the failures how you'd like.
    }

    public function rules(): array
    {

        return [
            'student_id'  =>[
                'required',
                'string',
                'unique:student_details'
            ],
            'roll_no' =>[
                'required',
                'integer'
            ],
            'name' => [
                'required',
                'string',
            ]

        ];
    }

}

我的主要目標是當學生插入「student_details」表時,將學生記錄插入具有外鍵「student_id」的所有關聯表中。 如果還有其他方法,請幫忙。

P粉312195700P粉312195700178 天前380

全部回覆(1)我來回復

  • P粉395056196

    P粉3950561962024-03-29 00:08:31

    而不是使用 Maatwebsite\Excel\Concerns\ToModel 您可以使用 Maatwebsite\Excel\Concerns\OnEachRow。您可以更好地控制每一行發生的情況。

    use App\StudentDetails;
    use Maatwebsite\Excel\Row;
    use Maatwebsite\Excel\Concerns\OnEachRow;
    
    class StudentsImport implements OnEachRow, ...
    {
        public function onRow(Row $row)
        {
            $rowIndex = $row->getIndex(); 
            $row = $row->toArray();
            // create model
            $studentDetails = StudentDetails::create([
                'roll_no' => $row['roll_no'],
                'student_id' => $row['student_id'],
                'div' => $row['div'],
                'name' => $row['name'],
                'gender' => $row['gender'],
                'user_key' => session()->get('user_id'),
                'group_key' => $group_key /* this variable doesn't seem to be defined anywhere */
            ]);
            // create related models
            $studentDetails->oral()->create([...]);
            $studentDetails->endsem()->create([...]);
        }
    }
    

    至於讓這一切在事務中發生:

    DB::transaction(fn () => (new StudentsImport)->import(...));
    

    回覆
    0
  • 取消回覆