Created
December 5, 2017 19:44
-
-
Save jagroop/5c3ec753fae3fc844c3cb738ae882c37 to your computer and use it in GitHub Desktop.
Import Excel Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers\Admin\Tournaments; | |
use App\Http\Controllers\Controller; | |
use App\Models\Tournament; | |
use Excel; | |
use Illuminate\Http\Request; | |
use Validator; | |
class TournamentsController extends Controller { | |
protected function getFile(Request $request) { | |
// Import a user provided file | |
$path = "uploads/admin/tournaments/sheets"; | |
$file = $request->file('tournaments'); | |
$fileName = str_random(30) . $file->getClientOriginalName(); | |
$file->move($path, $fileName); | |
// Return it's location | |
return $path . '/' . $fileName; | |
} | |
/** | |
* Display a listing of the tournament. | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function index() { | |
$tournaments = Tournament::latest()->paginate(50); | |
return view('admin.dashboard.tournaments.index', compact('tournaments')); | |
} | |
/** | |
* Show the form for creating a new tournament. | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function create() { | |
return view('admin.dashboard.tournaments.create'); | |
} | |
/** | |
* Store a newly created tournament in storage. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return \Illuminate\Http\Response | |
*/ | |
public function store(Request $request) { | |
$this->validate($request, [ | |
'tournaments' => 'required', | |
], ['tournaments.required' => 'Please select a Excel sheet of Tournaments.']); | |
//validate file extension | |
$file = $request->file('tournaments'); | |
$ext = strtolower($file->getClientOriginalExtension()); | |
$validator = Validator::make( | |
array('ext' => $ext), | |
array('ext' => 'in:xls,xlsx') | |
); | |
if ($validator->fails()) { | |
return redirect()->back()->with('error', 'Please select xls,xlsx format spreadsheet'); | |
} | |
try { | |
/* | |
Upload file | |
returns file path if successfully uploaded | |
*/ | |
$path = $this->getFile($request); | |
$excelReader = Excel::load($path)->get()->toArray(); | |
$tournaments = ($ext === "xls") ? $excelReader[1] : $excelReader; | |
foreach ($tournaments as $key => $cell) { | |
$event = $cell['event']; | |
$city = $cell['city']; | |
$state = $cell['state']; | |
$tournamentSlug = str_slug($event . "_" . $city . "_" . $state, '_'); | |
//check if already exists | |
$tournament = Tournament::firstOrNew(['tournament_slug' => $tournamentSlug]); | |
if ($tournament->exists === false && $event !== null) { | |
Tournament::firstOrCreate([ | |
'month_date' => $cell['month_date'] ?? null, | |
// 'month' => isset(explode(' ', trim($cell['month_date']))[0]) ? explode(' ', trim($cell['month_date']))[0] : null, | |
'month' => $cell['month'] ?? null, | |
'year' => $cell['year'] ?? null, | |
'event' => $cell['event'] ?? null, | |
'city' => $cell['city'] ?? null, | |
'state' => $cell['state'] ?? null, | |
'gender_category' => $cell['gender'] ?? null, | |
'age_limit' => $cell['age'] ?? null, | |
'age_from' => isset(explode('-', preg_replace('/\s+/', '', $cell['age']))[0]) ? explode('-', preg_replace('/\s+/', '', $cell['age']))[0] : null, | |
'age_to' => isset(explode('-', preg_replace('/\s+/', '', $cell['age']))[1]) ? explode('-', preg_replace('/\s+/', '', $cell['age']))[1] : null, | |
'country' => $cell['country'] ?? null, | |
'course' => $cell['course'] ?? null, | |
'entry_fee' => $cell['entry_fee'] ?? null, | |
'entry_deadline' => $cell['entry_deadline'] ?? null, | |
'field_size' => $cell['field_size'] ?? null, | |
'selection_criteria' => $cell['selection_criteria'] ?? null, | |
'website' => ($cell['website']) ?? null, | |
'zipcode' => $cell['zip_code'] ?? null, | |
'contact' => $cell['contact'] ?? null, | |
'tournament_slug' => $tournamentSlug, | |
'created_by' => 'admin', | |
'imported' => true, | |
]); | |
} | |
} | |
//Delete Uploaded Spreadsheet | |
\File::delete($path); | |
} catch (\Exception $e) { | |
return redirect()->back()->with('error', $e->getMessage()); | |
} | |
return redirect('admin/tournaments'); | |
} | |
/** | |
* Display the specified tournament. | |
* | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function show($id) { | |
// | |
} | |
/** | |
* Show the form for editing the specified tournament. | |
* | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function edit($id) { | |
// | |
} | |
/** | |
* Update the specified tournament in storage. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function update(Request $request, $id) { | |
// | |
} | |
/** | |
* Remove the specified tournament from storage. | |
* | |
* @param int $id | |
* @return \Illuminate\Http\Response | |
*/ | |
public function destroy($id) { | |
try { | |
Tournament::findOrFail($id)->delete(); | |
return redirect()->back()->with('success', 'Tournament deleted successfully'); | |
} catch (\Exception $e) { | |
return redirect()->back()->with('error', 'Error Occured while deleting tournament'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment