如何在透過 laravel excel 匯入 xlsx 檔案時執行自訂任務或觸發事件
P粉312195700
P粉312195700 2024-03-28 18:53:50
0
1
388

我是 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粉312195700
P粉312195700

全部回覆(1)
P粉395056196

而不是使用 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(...));
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!