初学者到Laravel,用array_chunk将记录导入到数据库中,当记录成功导入数据库时它的问题是,如果我回显该消息,那么它也不会重定向到路径中,然后它会打印出来5次我猜有3000条记录和600块大块。所以它的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";
}
});
}
}
}
答案 0 :(得分:0)
你的控制器的方法重定向应该在任何循环之外,它是控制器方法应该执行的最后一段代码
public function importExcel(Request $request)
{
// Your import code goes here
return redirect('inventoryadmin/dashboard')->with('message','Comic Records Successfully Imported');
}
答案 1 :(得分:0)
你在内存中制作了一个巨大的数组。你应该在每一行之后保存并将重定向放在循环之外,如下所示:
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');
}
}