【问题标题】:why when i import the excel file into laravel only one column is imported?为什么当我将excel文件导入laravel时只导入一列?
【发布时间】:2022-07-06 12:38:39
【问题描述】:

当我通过excel将数据导入laravel时,导入成功。但输入的数据只是顶行。谁能帮我解决一下?

这是我的 importTeacher**

<?php

namespace App\Imports;

use App\Models\Teacher;
use App\Models\Teachers;
use App\Models\User;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\ToModel;

class ImportTeacher implements ToCollection
{
    /**
     * @param array $row
     *
     * @return \Illuminate\Database\Eloquent\Model|null
     */
    public function collection(Collection $rows)
    {
        $id = DB::table('users')->orderBy('id', 'DESC')->limit(1)->get();
        foreach ($id as $i) {
            foreach ($rows as $row) {
                User::create([
                    'email' => $row[1],
                    'password' => bcrypt('guru123'),
                    'role' => 'teacher',
                    'status' => $row[6],
                ]);

                $id_user = $i->id;

                Teachers::create([
                    'user_id' => $id_user,
                    'NIP' => $row[2],
                    'name' => $row[3],
                    'phone' => $row[4],
                    'gender' => $row[5],
                    'birth_date' => $row[7],
                    'address' => $row[8],
                    'foto' => 'profile.png',
                ]);
                return redirect('/list-guru')->with('success', 'Berhasil menambahkan');
            }
        }
    }
}

当我通过excel将数据导入laravel时,导入成功。但输入的数据只是顶行。谁能帮我解决一下?

【问题讨论】:

    标签: excel laravel maatwebsite-excel


    【解决方案1】:
                return redirect('/list-guru')->with('success', 'Berhasil menambahkan');
    

    当你在 foreach 中使用 return 时,它会从循环中退出 你可以在 foreach 循环之后使用重定向

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多