【问题标题】:How to Import Excel with Table Relation in Laravel 7?如何在 Laravel 7 中导入带有表关系的 Excel?
【发布时间】:2021-11-13 20:36:27
【问题描述】:

我想问一下。

我想使用 laravel-excel 包制作 excel 导入功能。其中,我导入的数据与另一个表有关系值。

所以,当我导入 excel 时,它会将数据保存在两个表中。

第一个表,存储名称和文件路径等数据。而第二张表,保存了导入的excel文件的详细信息,并将关系添加到第一张表中。

下面是我的两张表

booked_vouchers:

  • 身份证
  • 姓名
  • 路径

booked_voucher_details:

  • 身份证
  • booked_voucher_id
  • 凭证代码
  • 课程代码
  • 用户名

以下是我在控制器中制作的代码,查看和导入文件。

表格

<form action="{{ route('booked.import')}}" method="POST" enctype="multipart/form-data">
    @csrf
    <div class="form-group">
        <label for="name">Name</label>
        <input name="name" type="text" class="form-control" required>
    </div>
    <div class="form-group">
        <label for="file">File</label>
        <input name="file" type="file" class="form-control" required>
    </div>
    <button type="submit" class="btn btn-primary">Import</button>
</form>

控制器

    public function import(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'file' => 'required|mimes:csv,xlx,xls,xlsx'
        ]);

        if ($validator->fails()) {
            return back()->with('toast_error', $validator->messages()->all()[0])->withInput();
        }

        $data = new BookedVoucher();

        $data->name = $request->name;
        $fileName = time().'_'.$request->file->getClientOriginalName();
        $filePath = $request->file('file')->storeAs('reports', $fileName, 'public');

        $data->file = $filePath;
        $data->created_by = \Auth::user()->id;

        if ($data->save()) {
            Excel::import(new BookedVoucherDetailImport, $request->file('file'));
        }

        return redirect()->back()->with('success','Data have been imported');
    }

BookedVoucherDetailImport.php

<?php

namespace App\Imports;

use App\Model\BookedVoucher;
use App\Model\BookedVoucherDetail;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithStartRow;

class BookedVoucherDetailImport implements ToModel, WithStartRow
{
    /**
     * @return int
     */
    public function startRow(): int
    {
        return 2;
    }
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        $data = BookedVoucher::first();
        return new BookedVoucherDetail([
            'booked_voucher_id'     => $data->id,
            'course_code'           => $row[0], 
            'voucher_code'          => $row[1],
            'user_name'         => $row[2],
            'status'                => 'OK'
        ]);
    }
}

如何设置booked_voucher_id的值与excel导入前已经保存的booked_voucher表的id值一致?

*目前,如果我使用类似 BookedVoucherDetailImport.php 文件中的代码,booked_voucher_details 表中的 booking_voucher_id 值的结果总是不正确的。

【问题讨论】:

    标签: php laravel package laravel-7


    【解决方案1】:

    您可以将 BookedVoucher 传递给 Import 类:

    Excel::import(new BookedVoucherDetailImport($data), $request->file('file'));
    

    然后将您的BookedVoucherDetailImport 更新为:

    <?php
    
    namespace App\Imports;
    
    use App\Model\BookedVoucher;
    use App\Model\BookedVoucherDetail;
    use Maatwebsite\Excel\Concerns\ToModel;
    use Maatwebsite\Excel\Concerns\WithStartRow;
    
    class BookedVoucherDetailImport implements ToModel, WithStartRow
    {
        /**
         * @var BookedVoucher 
         */
        protected $bookedVoucher;
    
        /**
         * @param BookedVoucher $bookedVoucher
         */
        public function __construct(BookedVoucher $bookedVoucher)
        {
            $this->bookedVoucher = $bookedVoucher;
        }
        
        /**
         * @return int
         */
        public function startRow(): int
        {
            return 2;
        }
    
        /**
         * @param array $row
         *
         * @return \Illuminate\Database\Eloquent\Model|null
         */
        public function model(array $row)
        {
            return new BookedVoucherDetail([
                'booked_voucher_id' => $this->bookedVoucher->id,
                'course_code'       => $row[0],
                'voucher_code'      => $row[1],
                'user_name'         => $row[2],
                'status'            => 'OK',
            ]);
        }
    }
    

    【讨论】:

    • 感谢@Rwd,这真的很有帮助,我试过了,效果很好。
    • @HilmiHidayat 很高兴我能帮上忙! :)
    猜你喜欢
    • 2021-11-21
    • 2020-07-09
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 2019-10-29
    相关资源
    最近更新 更多