【发布时间】:2020-02-05 22:03:45
【问题描述】:
大家好,
我正在使用Laravel Excel/Maatwebsite 开发一个系统。我想要实现的是当用户将一个excel文件插入系统时,系统会检查一些东西,然后将数据插入到数据库中。
这是数据库模式的一个实例:
Hadees:
h_id | h_english | r_id | h_last_raavi_id | b_id | s_id | k_id | h_status
Raavi:
r_id | r_english | r_status
Baab:
b_id | b_english | s_id | b_status
Section:
s_id | s_english | k_id | s_status
Kitaab:
k_id | k_english | k_status
我的控制器:
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Section;
use App\Models\Raavi;
use App\Imports\HadeesImport;
use Excel;
class ImportHadeesController extends Controller{
/**
* Show the application import-hadees page.
*
* @return \Illuminate\Http\Response
*/
public function index(){
$section = Section::where(['s_status' => 1])->get();
return view('admin/import-hadees', compact('section'));
}
/**
* This method uses the Excel facade to prep the excel file
* and extract data from it and uses App\Imports\HadeesImport
* class to insert each row in the database schema accordingly.
*
* @param Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function importHadees(Request $request){
$raavi = Raavi::where(['r_status' => 1])->get();
if($request->file('hadees_sheet')) {
} else {
return response()->json(['status'=>'error', 'msg'=> 'No file present!']);
}
$temp = $request->file('hadees_sheet')->store('temp');
$path=storage_path('app').'/'.$temp;
$hadees = Excel::import(new HadeesImport($request->s_id, compact('raavi')), $path);
if($hadees){
return response()->json(['status'=>'success', 'msg'=> 'Successfully imported all the data from the file to the database!']);
} else{
return response()->json(['status'=>'error', 'msg'=> 'Unable to import data from the file to the database!']);
}
}
}
HadeesImport 类:
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use App\Models\Section;
use App\Models\Baab;
use App\Models\Hadees;
use App\Models\Raavi;
class HadeesImport implements ToCollection, WithHeadingRow{
/**
* Global variable for section_id.
*/
public $s_id;
/**
* Global variable for raavi's data.
*/
public $raavi;
/**
* Construction function.
*
* @param int $id
*/
function __construct($id, $arr) {
$this->s_id = $id;
$this->raavi = $arr;
}
/**
* This method is responsible for inserting the excel
* sheet's rows data to the database schema.
*
* @param Collection $row
*/
public function collection(Collection $rows){
$baab = Baab::where(['s_id' => $this->s_id])->get();
$hissa = Section::where(['s_id' => $this->s_id])->first();
$kitaab = $hissa->k_id;
$first_raavi = 0;
$last_raavi = 0;
$baab_id = 0;
$data = array();
foreach ($rows as $row){
if($row['hadees_arabic'] != "" && $row['hadees_urdu'] != ""){
$baab_id = $this->baabCheck($baab, $row);
foreach($this->raavi['raavi'] as $rav){
if(trim($rav->r_english) == trim($row['first_raavi_english'])){
$first_raavi = $rav->r_id;
} else{
$first_raavi = 0;
}
$last_raavi = (trim($rav->r_english) == trim($row['last_raavi_english']))? $rav->r_id : 0;
}
if($first_raavi == 0){
$raavi = Raavi::create([
'r_arabic' => trim($row['first_raavi_urdu']),
'r_urdu' => trim($row['first_raavi_urdu']),
'r_english' => trim($row['first_raavi_english']),
'r_nickname' => trim($row['raavi_other_names']),
'r_status' => 1,
]);
$first_raavi = $raavi->r_id;
}
if($last_raavi == 0){
$raavi = Raavi::create([
'r_arabic' => trim($row['last_raavi_urdu']),
'r_urdu' => trim($row['last_raavi_urdu']),
'r_english' => trim($row['last_raavi_english']),
'r_nickname' => 'n/a',
'r_status' => 1,
]);
$last_raavi = $raavi->r_id;
}
$data = array([
'h_arabic' => trim($row['hadees_arabic']),
'h_urdu' => trim($row['hadees_urdu']),
'h_english' => trim($row['hadees_english']),
'h_roman_keywords' => trim($row['roman_keywords']),
'h_number' => trim($row['hadees_number']),
'r_id' => $first_raavi,
'h_last_raavi_id' => $last_raavi,
'b_id' => $baab_id,
's_id' => $this->s_id,
'k_id' => $kitaab,
'h_status' => 1
]);
}
}
$hadees = Hadees::create($data);
}
/**
* Checks if the baab exists in the database or not.
*
* @param Collection $baab
* @param Object $row
* @return int - baad_id or 0
*/
public function baabCheck($baab, $row){
foreach($baab as $b){
if(trim($b->b_arabic) == trim($row['baab_arabic']) || trim($b->b_urdu) == trim($row['baab_urdu']) || trim($b->b_english) == trim($row['baab_english'])){
return $b->b_id;
} else{
return 0;
}
}
}
}
当数据较少时,一切正常。现在我在 Raavi 表中有 1400+ 行,在 Baab 表中有 10,000+ 行。现在,每当我尝试将工作表导入系统时,它都会给我这个错误:
允许的内存大小为 268435456 字节已用尽(尝试分配 37748736 字节)。
我认为这是因为 foreach() 循环太长了。任何形式的帮助将不胜感激。如果你们对糟糕的编码或糟糕的逻辑构建有任何建议,请告诉我。我已经在这个问题上停留了将近两天。提前致谢。
P.s:本地主机和主机上的错误相同,只是字节数不同。这是由于不同的 memory_limit 设置,我相信。
【问题讨论】:
标签: php laravel localhost hosting maatwebsite-excel