亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

如何在透過 laravel excel 匯入 xlsx 檔案時(shí)執(zhí)行自訂任務(wù)或觸發(fā)事件
P粉312195700
P粉312195700 2024-03-28 18:53:50
0
1
690

我是 Laravel 新手, 我想從 xlsx 檔案將學(xué)生詳細(xì)資訊插入 mysql 資料庫。 我使用 Laravel excel v3 導(dǎo)入 excel 檔。它運(yùn)行良好。但是,除了在 1 個(gè)表中插入學(xué)生詳細(xì)資料之外,還應(yīng)在所有關(guān)聯(lián)表中建立相同的學(xué)生 ID 記錄。

範(fàn)例--> 如果在「student_details」表中插入 1 個(gè)學(xué)生,則必須在「oral」和「endsem」表中建立 1 筆記錄,外鍵為「student_id」。

我已經(jīng)舉辦了活動(dòng),在口頭表和最終表中記錄這些記錄。 現(xiàn)在的問題是如何應(yīng)用該事件以及如何在創(chuàng)建學(xué)生以觸發(fā)事件後獲取學(xué)生 ID。 (學(xué)生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',
            ]

        ];
    }

}

我的主要目標(biāo)是當(dāng)學(xué)生插入「student_details」表時(shí),將學(xué)生記錄插入具有外鍵「student_id」的所有關(guān)聯(lián)表中。 如果還有其他方法,請(qǐng)幫忙。

P粉312195700
P粉312195700

全部回覆(1)
P粉395056196

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

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([...]);
    }
}

至於讓這一切在事務(wù)中發(fā)生:

DB::transaction(fn () => (new StudentsImport)->import(...));
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板