You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
5.5 KiB
PHTML

6 years ago
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Siswa;
use App\Models\Kelas;
use Illuminate\Support\Facades\Input;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\SiswaExport;
use App\Imports\SiswaImport;
use Complex\Exception;
6 years ago
class SiswaController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
6 years ago
{
$q = $request->get('q');
if($q == null){
$siswa = Siswa::orderBy('created_at','desc')->paginate(15);
}else{
$siswa = Siswa::where('nama','like','%'.$q.'%')->orderBy('created_at','desc')->paginate(15);
}
return view('siswa.index', [
'siswa' => $siswa->appends(Input::except('page'))
]);
6 years ago
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$kelas = Kelas::all();
return view('siswa.form', ['kelas' => $kelas]);
6 years ago
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'kelas_id' => 'required|numeric',
'nama' => 'required|max:255',
'tempat_lahir' => 'nullable|max:255',
'tanggal_lahir' => 'nullable|date',
'jenis_kelamin' => 'nullable|in:L,P',
'alamat' => 'nullable',
'nama_wali' => 'nullable|max:255',
'telp_wali' => 'nullable|numeric',
'is_yatim' => 'nullable|boolean',
]);
$siswa = Siswa::make($request->input());
if($request->is_yatim != null){
$siswa->is_yatim = 1;
}
if($siswa->save()){
return redirect()->route('siswa.index')->with([
'type' => 'success',
'msg' => 'Siswa ditambahkan'
]);
}else{
return redirect()->route('siswa.index')->with([
'type' => 'danger',
'msg' => 'Err.., Terjadi Kesalahan'
]);
}
6 years ago
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show(Siswa $siswa)
6 years ago
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit(Siswa $siswa)
6 years ago
{
$kelas = Kelas::all();
return view('siswa.form', [
'siswa' => $siswa,
'kelas' => $kelas
]);
6 years ago
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Siswa $siswa)
6 years ago
{
$request->validate([
'kelas_id' => 'required|numeric',
'nama' => 'required|max:255',
'tempat_lahir' => 'nullable|max:255',
'tanggal_lahir' => 'nullable|date',
'jenis_kelamin' => 'nullable|in:L,P',
'alamat' => 'nullable',
'nama_wali' => 'nullable|max:255',
'telp_wali' => 'nullable|numeric',
'is_yatim' => 'nullable|boolean',
]);
$siswa = $siswa->fill($request->input());
if($request->is_yatim != null){
$siswa->is_yatim = 1;
}else{
$siswa->is_yatim = 0;
}
if($siswa->save()){
return redirect()->route('siswa.index')->with([
'type' => 'success',
'msg' => 'Siswa diubah'
]);
}else{
return redirect()->route('siswa.index')->with([
'type' => 'danger',
'msg' => 'Err.., Terjadi Kesalahan'
]);
}
6 years ago
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(Siswa $siswa)
6 years ago
{
return redirect()->route('siswa.index')->with([
'type' => 'danger',
'msg' => 'Err.., Belum dibikin cuy , cocokin sama tagihan dll dulu'
]);
}
public function showFormImport()
{
return view('siswa.import');
}
public function import(Request $request)
{
$request->validate([
'file' => 'required'
]);
if($request->hasFile('file')){
$extensions = ["xls","xlsx","xlm","xla","xlc","xlt","xlw"];
$result = [$request->file('file')->getClientOriginalExtension()];
if(in_array($result[0], $extensions)){
Excel::import(new SiswaImport, $request->file('file'));
return redirect()->route('siswa.index')->with([
'type' => 'success',
'msg' => 'data berhasil di import'
]);
}
return redirect()->route('siswa.index')->with([
'type' => 'danger',
'msg' => 'terjadi kesalahan : file error'
]);
}
return redirect()->route('siswa.index')->with([
'type' => 'info',
'msg' => 'terjadi kesalahan : nofile'
]);
}
public function export()
{
return Excel::download(new SiswaExport, 'siswa-'.now().'.xlsx');
6 years ago
}
}