filter($request->all()) ->whereHas('corporatePlan', function ($corporatePlan) use ($corporate_id) { $corporatePlan->where('corporate_id', $corporate_id); }) ->with('corporatePlan') ->paginate(0) ->appends($request->all()); return $plans; } /** * Show the form for creating a new resource. * @return Renderable */ public function create() { return view('internal::create'); } /** * Store a newly created resource in storage. * @param Request $request * @return Renderable */ public function store(Request $request) { // } /** * Show the specified resource. * @param int $id * @return Renderable */ public function show($id) { return view('internal::show'); } /** * Show the form for editing the specified resource. * @param int $id * @return Renderable */ public function edit($id) { return view('internal::edit'); } /** * Update the specified resource in storage. * @param Request $request * @param int $id * @return Renderable */ public function update(Request $request, $id) { // } /** * Remove the specified resource from storage. * @param int $id * @return Renderable */ public function destroy($id) { // } public function planImport(Request $request, $corporate_id) { $request->validate([ 'file' => 'required|file|mimes:xls,xlsx,csv,txt', ]); $file_name = now()->getPreciseTimestamp(3).'-'.$request->file('file')->getClientOriginalName(); $file = $request->file('file')->storeAs('temp', $file_name); // return false; $reader = ReaderEntityFactory::createReaderFromFile(Storage::path('temp/'.$file_name)); $reader->open(Storage::path('temp/'.$file_name)); $headers_map_to_table_fields = Plan::$doc_headers_to_field_map; $imported_plan_data = 0; $failed_plan_data = []; foreach ($reader->getSheetIterator() as $sheet) { $doc_headers_indexes = []; foreach ($sheet->getRowIterator() as $index => $row) { if ($index == 1) { // First Row Must be Header foreach ($row->getCells() as $index => $cell) { $doc_headers_indexes[$index] = $cell->getValue(); } } else { // Next Row Should be Data $new_plan_data = []; foreach ($row->getCells() as $index => $cell) { $new_plan_data[$headers_map_to_table_fields[$doc_headers_indexes[$index]]] = $cell->getValue(); } // $imported_plan_data[] = $new_row; // Insert to Array // Create Directly try { Plan::updateOrCreate([ 'code' => $new_plan_data['code'] ], $new_plan_data); $imported_plan_data++; } catch(\Exception $e) { $failed_plan_data[] = $new_plan_data; } } } break; //only read first sheet } $reader->close(); Storage::delete('temp/'.$file_name); // throw(404); return [ 'total_successed_row' => $imported_plan_data, 'total_failed_row' => count($failed_plan_data), 'failed_row' => $failed_plan_data ]; } }