gpt4 book ai didi

php - 使用 array_chunk 将记录导入数据库后,Laravel 返回重定向不起作用

转载 作者:行者123 更新时间:2023-11-29 18:00:35 24 4
gpt4 key购买 nike

Laravel 初学者,使用 array_chunk 将记录导入数据库,当记录成功导入到数据库中时出现问题,它不会在路径中重定向,而且如果我回显消息,那么它会打印 5 次,我猜有 3000 条记录和 600 block 。所以它是 6 次回显。

请帮助我,我不知道代码出了什么问题,也不知道我在这里错过了什么

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Item;
use App\Master;
use Excel;
use Validator;
use Session;
use Image;
use App\Http\Requests;
use DB;
use Auth;
use SuperClosure\Serializer;

class MaatwebsiteDemoController extends Controller
{


public function importExcel(Request $request)
{

if($request->hasFile('sample_file')){

$path = $request->file('sample_file')->getRealPath();
Excel::filter('chunk')->load($path)->chunk(600, function($results)
{
foreach ($results as $value)
{

$arr[] = ['DIAMD_NO' => $value->diamd_no,
'FULL_TITLE' => $value->full_title,
'MAIN_DESC' => $value->main_desc,
'VARIANT_DESC' => $value->variant_desc,
'ISSUE_NO' => $value->issue_no,
'ISSUE_SEQ_NO' => $value->issue_seq_no,
'PRICE' => $value->price,
'PUBLISHER' => $value->publisher,
'CARDS_PER_PACK' => $value->cards_per_pack,
'PACK_PER_BOX' => $value->pack_per_box,
'PRNT_DATE' => $value->prnt_date,
'FOC_VENDOR' => $value->foc_vendor,
'SHIP_DATE' => $value->ship_date,
'SRP' => $value->srp,
'CATEGORY' => $value->category,
'GENRE' => $value->genre,
'MATURE' => $value->mature,
'ADULT' => $value->adult,
'NOTE_PRICE' => $value->note_price,
'PAGE' => $value->page,
'WRITER' => $value->writer,
'ARTIST' => $value->artist,
'COVER_ARTIST' => $value->cover_artist,
'COLORIST' => $value->colorist,
'FOC_DATE' => $value->foc_date,
'OFFERED_DATE' => $value->offered_date,
'URL'=> $value->url,
'created_by' =>Auth::guard('inventoryadmin')->user()->id
];

}
//$count = count($arr);
if(!empty(arr))
{
\DB::table('master_comic')->insert($arr);
// return redirect('inventoryadmin/dashboard')->with('message','Comic Records Successfully Imported');
echo "Successfully imported";
}
});


}
}
}

最佳答案

你在内存中创建了一个巨大的数组。您应该在每一行之后保存并将重定向放在循环之外,如下所示:

public function importExcel(Request $request)
{

if ($request->hasFile('sample_file')) {

$path = $request->file('sample_file')->getRealPath();
Excel::filter('chunk')->load($path)->chunk(600, function ($results) {
foreach ($results as $value) {

$arr = [
'DIAMD_NO' => $value->diamd_no,
'FULL_TITLE' => $value->full_title,
'MAIN_DESC' => $value->main_desc,
'VARIANT_DESC' => $value->variant_desc,
'ISSUE_NO' => $value->issue_no,
'ISSUE_SEQ_NO' => $value->issue_seq_no,
'PRICE' => $value->price,
'PUBLISHER' => $value->publisher,
'CARDS_PER_PACK' => $value->cards_per_pack,
'PACK_PER_BOX' => $value->pack_per_box,
'PRNT_DATE' => $value->prnt_date,
'FOC_VENDOR' => $value->foc_vendor,
'SHIP_DATE' => $value->ship_date,
'SRP' => $value->srp,
'CATEGORY' => $value->category,
'GENRE' => $value->genre,
'MATURE' => $value->mature,
'ADULT' => $value->adult,
'NOTE_PRICE' => $value->note_price,
'PAGE' => $value->page,
'WRITER' => $value->writer,
'ARTIST' => $value->artist,
'COVER_ARTIST' => $value->cover_artist,
'COLORIST' => $value->colorist,
'FOC_DATE' => $value->foc_date,
'OFFERED_DATE' => $value->offered_date,
'URL' => $value->url,
'created_by' => Auth::guard('inventoryadmin')->user()->id,
];
\DB::table('master_comic')->insert($arr);

}
});

return redirect('inventoryadmin/dashboard')->with('message', 'Comic Records Successfully Imported');

}
}

关于php - 使用 array_chunk 将记录导入数据库后,Laravel 返回重定向不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48396670/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com