简体   繁体   English

如何从 Excel 导入表中获取 Laravel 最后插入的 ID

[英]How to get Laravel last inserted ID from Excel import sheet

I'm relatively new to Laravel (historically I've written all my PHP from scratch) just trying to learn the basics Laravel site creation before making a new real site.我对 Laravel 比较陌生(从历史上看,我已经从头开始编写了所有的 PHP)只是想在创建新的真实站点之前学习基本的 Laravel 站点创建。 Right now I have a multi-sheet excel file that I want to import and place the data into tables.现在我有一个多页 excel 文件,我想导入该文件并将数据放入表中。 I followed the Laravel Excel instructions and I have this ChartImport.php file that works well exactly as written/expected.我按照Laravel Excel 说明进行操作,并且我有这个 ChartImport.php 文件,该文件完全符合书面/预期的要求。

<?php

namespace App\Imports;

use App\Chart;
use App\Chart_count;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;

class ChartImport implements WithMultipleSheets
{

    public function sheets(): array
    {
        return [
            0 => new ChartNameImport(),
            1 => new ChartCountImport(),
        ];
    }
}

class ChartNameImport implements ToModel
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        return new Chart([
            'title' => $row[0],
            'owner_id' => auth()->id()
        ]);
    }
}


class ChartCountImport implements ToModel
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        return new Chart_count([
            'chart_id' => 3,
            'count' => $row[0],
            'name' => $row[1],
        ]);
    }
}

Though right now, as you can see, I'm hardcoding虽然现在,如您所见,我正在硬编码

            'chart_id' => 3

I want the chart_id to be the automatically created id of the previous function call, but I'm not sure if there's any Laravel way to go about it.我希望 chart_id 是之前 function 调用的自动创建的 id,但我不确定是否有任何 Laravel 方式到 go 关于它。 I'm sure I could probably create a new PDO instance and grab the data from the database or something, but that doesn't seem like a good way at all.我确信我可以创建一个新的 PDO 实例并从数据库或其他东西中获取数据,但这似乎根本不是一个好方法。 How would you go about doing it?你会怎么做呢? Thanks!谢谢!

Instead of importing to models you can import to collections :您可以导入到collections ,而不是导入模型:

example:例子:

namespace App\Imports;

use App\User;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;

class UsersImport implements ToCollection
{
    public function collection(Collection $rows)
    {
        foreach ($rows as $row) 
        {
            $id = DB::table('users')->insertGetId([
                'title' => $row[0],
                'owner_id' => auth()->id()
            ]);
        }
    }
}

notice i am using the [insertGetId] method to insert a record and then retrieve the ID.请注意,我正在使用[insertGetId]方法插入记录,然后检索 ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM