Export Import Data User
This commit is contained in:
parent
b52222acff
commit
be15f741bc
|
@ -2,15 +2,37 @@
|
||||||
|
|
||||||
namespace App\Exports;
|
namespace App\Exports;
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
use Maatwebsite\Excel\Concerns\FromCollection;
|
use Maatwebsite\Excel\Concerns\FromCollection;
|
||||||
|
use Maatwebsite\Excel\Concerns\WithHeadings;
|
||||||
|
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
|
||||||
|
|
||||||
class UserExport implements FromCollection
|
class UserExport implements FromCollection, WithHeadings, ShouldAutoSize
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @return \Illuminate\Support\Collection
|
* @return \Illuminate\Support\Collection
|
||||||
*/
|
*/
|
||||||
public function collection()
|
public function collection()
|
||||||
{
|
{
|
||||||
//
|
return User::all()->map(function ($user, $index) {
|
||||||
|
return [
|
||||||
|
'No' => $index + 1,
|
||||||
|
'Name' => $user->name,
|
||||||
|
'Username' =>$user->username,
|
||||||
|
'Email' => $user->email,
|
||||||
|
'Role' => $user->role,
|
||||||
|
];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function headings(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'No',
|
||||||
|
'Nama',
|
||||||
|
'Username',
|
||||||
|
'Email',
|
||||||
|
'Role',
|
||||||
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Exports;
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use Maatwebsite\Excel\Concerns\FromCollection;
|
||||||
|
|
||||||
|
class UserTemplateExport implements FromCollection
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return \Illuminate\Support\Collection
|
||||||
|
*/
|
||||||
|
public function collection()
|
||||||
|
{
|
||||||
|
return collect([
|
||||||
|
['No', 'Nama Siswa'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function headings(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'name',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,6 +4,10 @@
|
||||||
|
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use App\Exports\UserExport;
|
||||||
|
use App\Imports\UserImport;
|
||||||
|
use App\Exports\UserTemplateExport;
|
||||||
|
use Maatwebsite\Excel\Facades\Excel;
|
||||||
use Illuminate\Support\Facades\Validator;
|
use Illuminate\Support\Facades\Validator;
|
||||||
|
|
||||||
class DataUserController extends Controller
|
class DataUserController extends Controller
|
||||||
|
@ -11,7 +15,26 @@ class DataUserController extends Controller
|
||||||
/**
|
/**
|
||||||
* Display a listing of the resource.
|
* Display a listing of the resource.
|
||||||
*/
|
*/
|
||||||
public function index()
|
public function userexport(){
|
||||||
|
return Excel::download(new UserExport,'data-user.xlsx');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function userimport(Request $request)
|
||||||
|
{
|
||||||
|
$file = $request->file('file');
|
||||||
|
$nameFile = $file->getClientOriginalName();
|
||||||
|
$file->move('DataUser', $nameFile);
|
||||||
|
|
||||||
|
Excel::import(new UserImport, public_path('/DataUser/'.$nameFile));
|
||||||
|
return redirect('user')->with('success', 'Data Berhasil Di Import!');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function downloadUserTemplate()
|
||||||
|
{
|
||||||
|
return Excel::download(new UserTemplateExport, 'data-user-template.xlsx');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function index()
|
||||||
{
|
{
|
||||||
$users = User::all();
|
$users = User::all();
|
||||||
return view('user.user', compact('users'));
|
return view('user.user', compact('users'));
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Imports;
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
use Maatwebsite\Excel\Concerns\ToModel;
|
||||||
|
use Maatwebsite\Excel\Concerns\WithStartRow;
|
||||||
|
|
||||||
|
class UserImport implements ToModel, WithStartRow
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param array $row
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Database\Eloquent\Model|null
|
||||||
|
*/
|
||||||
|
public function model(array $row)
|
||||||
|
{
|
||||||
|
$name = $row[1];
|
||||||
|
$username = explode(' ', $name)[0];
|
||||||
|
$email = strtolower($username) . '@gmail.com';
|
||||||
|
$password = Hash::make('man3bwi');
|
||||||
|
$role = 'siswa';
|
||||||
|
|
||||||
|
return new User([
|
||||||
|
'name' => $name,
|
||||||
|
'username' => $username,
|
||||||
|
'email' => $email,
|
||||||
|
'password' => $password,
|
||||||
|
'role' => $role,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function startRow(): int
|
||||||
|
{
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -160,6 +160,7 @@
|
||||||
* Package Service Providers...
|
* Package Service Providers...
|
||||||
*/
|
*/
|
||||||
RealRashid\SweetAlert\SweetAlertServiceProvider::class,
|
RealRashid\SweetAlert\SweetAlertServiceProvider::class,
|
||||||
|
Maatwebsite\Excel\ExcelServiceProvider::class,
|
||||||
/*
|
/*
|
||||||
* Application Service Providers...
|
* Application Service Providers...
|
||||||
*/
|
*/
|
||||||
|
@ -185,5 +186,6 @@
|
||||||
// 'Example' => App\Facades\Example::class,
|
// 'Example' => App\Facades\Example::class,
|
||||||
])->toArray(),
|
])->toArray(),
|
||||||
'Alert' => RealRashid\SweetAlert\Facades\Alert::class,
|
'Alert' => RealRashid\SweetAlert\Facades\Alert::class,
|
||||||
|
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
|
||||||
|
|
||||||
];
|
];
|
||||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -39,10 +39,10 @@
|
||||||
<!-- Button trigger modal -->
|
<!-- Button trigger modal -->
|
||||||
<button type="button" class="btn btn-primary mx-2" data-toggle="modal" data-target="#add-user">
|
<button type="button" class="btn btn-primary mx-2" data-toggle="modal" data-target="#add-user">
|
||||||
<i class="icon dw dw-add"></i> Tambah User</button>
|
<i class="icon dw dw-add"></i> Tambah User</button>
|
||||||
<button type="button" class="btn btn-primary mx-2" data-toggle="modal" data-target="#add-user">
|
<button type="button" class="btn btn-primary mx-2" data-toggle="modal" data-target="#import-user">
|
||||||
<i class="icon dw dw-download"></i> Import User</button>
|
<i class="icon dw dw-download"></i> Import User</button>
|
||||||
<button type="button" class="btn btn-primary mx-2" data-toggle="modal" data-target="#add-user">
|
<a href="/export-user"><button type="button" class="btn btn-primary mx-2">
|
||||||
<i class="icon dw dw-upload"></i> Export User</button>
|
<i class="icon dw dw-upload"></i> Export User</button></a>
|
||||||
<form class="form-inline ml-auto" action="{{ route('user.index') }}" method="GET">
|
<form class="form-inline ml-auto" action="{{ route('user.index') }}" method="GET">
|
||||||
<div class="input-group">
|
<div class="input-group">
|
||||||
<input type="text" class="form-control" name="search" placeholder="Search">
|
<input type="text" class="form-control" name="search" placeholder="Search">
|
||||||
|
@ -246,14 +246,6 @@ class="badge btn-primary" data-toggle="modal"
|
||||||
<option value="{{ $role }}" {{ $user->role == $role ? 'selected' : '' }}>{{ $role }}</option>
|
<option value="{{ $role }}" {{ $user->role == $role ? 'selected' : '' }}>{{ $role }}</option>
|
||||||
@endforeach
|
@endforeach
|
||||||
</select>
|
</select>
|
||||||
{{-- <select class="form-control" id="role" name="role" required>
|
|
||||||
<option value="" disabled>Pilih Role</option>
|
|
||||||
@foreach ($users as $user)
|
|
||||||
<option value="{{ $user->id }}" {{ old('role') == $user->id ? 'selected' : '' }}>
|
|
||||||
{{ $user->role }}
|
|
||||||
</option>
|
|
||||||
@endforeach
|
|
||||||
</select> --}}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -292,5 +284,41 @@ class="badge btn-primary" data-toggle="modal"
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- Modal Import-->
|
||||||
|
<div class="modal fade center-modal" id="import-user" tabindex="-1" aria-labelledby="exampleModalLabel"
|
||||||
|
aria-hidden="true">
|
||||||
|
<div class="modal-dialog">
|
||||||
|
<div class="modal-content">
|
||||||
|
<div class="modal-header">
|
||||||
|
<h5 class="modal-title" id="exampleModalLabel">Import User</h5>
|
||||||
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||||
|
</div>
|
||||||
|
<div class="modal-body">
|
||||||
|
<form action="{{ route('import-user') }}" class="needs-validation" novalidate=""
|
||||||
|
method="POST" enctype="multipart/form-data">
|
||||||
|
@csrf
|
||||||
|
<div class="card-body">
|
||||||
|
<div class="form-group row">
|
||||||
|
<div class="col-sm-12">
|
||||||
|
<input type="file" class="form-control" id="file" name="file"
|
||||||
|
required="">
|
||||||
|
<div class="invalid-feedback">
|
||||||
|
Tolong upload sebuah file!
|
||||||
|
</div>
|
||||||
|
<label class="col-sm-12 col-form-label">- Upload file dengan bentuk format (.xlxs) </label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="modal-footer">
|
||||||
|
<a href="{{ route('download-user-template') }}" class="btn btn-info mb-2">Unduh Template</a>
|
||||||
|
<button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
|
||||||
|
<button type="submit" class="btn btn-primary">Simpan</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
@endforeach
|
@endforeach
|
||||||
@endsection
|
@endsection
|
||||||
|
|
|
@ -96,6 +96,9 @@
|
||||||
// Route::post('/subkriteria/calculate', [SubCriteriaController::class, 'calculateBobotSubKriteria'])->name('subkriteria.calculate');
|
// Route::post('/subkriteria/calculate', [SubCriteriaController::class, 'calculateBobotSubKriteria'])->name('subkriteria.calculate');
|
||||||
|
|
||||||
Route::resource('user', DataUserController::class);
|
Route::resource('user', DataUserController::class);
|
||||||
|
Route::get('/export-user',[DataUserController::class, 'userexport'])->name('export-user');
|
||||||
|
Route::post('/import-user',[DataUserController::class, 'userimport'])->name('import-user');
|
||||||
|
Route::get('/download-user-template', [DataUserController::class, 'downloadUserTemplate'])->name('download-user-template');
|
||||||
|
|
||||||
Route::post('/kriteria/calculate', [PerhitunganController::class, 'calculateBobotKriteria'])->name('kriteria.calculate');
|
Route::post('/kriteria/calculate', [PerhitunganController::class, 'calculateBobotKriteria'])->name('kriteria.calculate');
|
||||||
Route::post('/subkriteria/calculate', [PerhitunganController::class, 'calculateBobotSubKriteria'])->name('subkriteria.calculate');
|
Route::post('/subkriteria/calculate', [PerhitunganController::class, 'calculateBobotSubKriteria'])->name('subkriteria.calculate');
|
||||||
|
|
Loading…
Reference in New Issue