Şu şekilde dener misiniz?
Farzedelim excel dosyanızda name, email ve telephone adında 3 adet kolon mevcut.
Öncelikle aşağıdaki repoyu indirip app klasörünün içerisine Excel isminde bir klasör oluşturup içerisine SimpleXLSX.php dosyasını ekleyiniz.
https://github.com/shuchkin/simplexlsx
app->Excel->SimpleXLSX.php
View Bölümü
<form action="{{ route('data.import') }}" method="POST" enctype="multipart/form-data">
@csrf
<input type="file" name="data" class="form-control">
<button class="btn btn-success" type="submit" name="submit" value="Submit">Dataları Aktar</button>
</form>
Route Bölümü
Route::post('import', 'DataController@import')->name('data.import');
Controller Bölümü
use SimpleXLSX; ("namespace" tanımlamanızın altına eklemeyi unutmayın!)
public function import(Request $request)
{
require (base_path('app/Excel/SimpleXLSX.php'));
if ($request->hasFile('data')) {
$file = $request->file('data');
if ( $xlsx = SimpleXLSX::parse($file) ) {
$header_values = $rows = [];
foreach ( $xlsx->rows() as $k => $r ) {
if ( $k === 0 ) {
$header_values = $r;
continue;
}
$rows[] = array_combine( $header_values, $r );
}
$allData = $rows;
if(count($allData) > 0) {
foreach($allData as $data) {
$importData[] = [
'name' => $data['name'],
'telephone' => $data['telephone'],
'email' => $data['email'],
];
}
}
DB::table('data')->insert($importData);
return back()->with('message', 'Datalar başarıyla aktarıldı.');
} else {
return back()->with('message', SimpleXLSX::parseError());
}
}
}