当我通过excel导入数据到laravel时,导入成功。但是输入的数据只是最上面一行。有人能帮我解决这个问题吗?
This my importTeacher**
<?php
namespace AppImports;
use AppModelsTeacher;
use AppModelsTeachers;
use AppModelsUser;
use IlluminateSupportCollection;
use IlluminateSupportFacadesDB;
use MaatwebsiteExcelConcernsToCollection;
use MaatwebsiteExcelConcernsToModel;
class ImportTeacher implements ToCollection
{
/**
* @param array $row
*
* @return IlluminateDatabaseEloquentModel|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时,导入成功。但是输入的数据只是最上面一行。有人能帮我解决这个问题吗?
return redirect('/list-guru')->with('success', 'Berhasil menambahkan');
当你在foreach中使用return时,它会从循环中退出可以在foreach循环
之后使用重定向