Index
Add Siswa
Code Web :
Route::get('/', 'SiswaController@index'); Route::post('/tambah', 'SiswaController@tambah'); Route::get('/edit/{id}', 'SiswaController@edit'); Route::post('/update/{id}', 'SiswaController@update'); Route::get('/hapus/{id}', 'SiswaController@hapus'); Route::get('/cari', 'SiswaController@cari');
Code View Index :
<!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <title>Data Siswa</title> </head> <body> <div class="container"> <div class="card"> <div class="card-header text-center"> <strong><a href="/">Data Siswa</a></strong> </div> <div class="card-body"> <div class="row"> <div class="col-sm"> <!-- Button trigger modal --> <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#inputSiswaBaru"> Input Siswa Baru </button> </div> <div class="col-sm"> <form action="/cari" method="GET"> <input type="text" name="cari" placeholder="Cari Siswa .." value="{{ old('cari') }}"> <input type="submit" value="CARI"> </form> </div> </div> <br> <table class="table" style="text-transform: uppercase;"> <thead class="thead-dark"> <tr> <th scope="col">#</th> <th scope="col">Nama</th> <th scope="col">No Tlp</th> <th scope="col">Alamat</th> <th scope="col">Aksi</th> </tr> </thead> <tbody> <?php $no=1;?> @foreach ($siswa as $s) <tr> <th scope="row"> {{ $no }} </th> <td> {{ $s->nama }} </td> <td> +62 {{ $s->tlp }} </td> <td> {{ $s->alamat }} </td> <td> <a href="/edit/{{ $s->id }}" class="btn btn-warning btn-sm">Edit</a> <a href="/hapus/{{ $s->id }}" class="btn btn-danger btn-sm">Hapus</a> </td> </tr> <?php $no++?> @endforeach </tbody> </table> {{ $siswa->links() }} </div> </div> <!-- Modal --> <div class="modal fade" id="inputSiswaBaru" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLabel">Input Siswa Baru</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <div class="modal-body"> <form method="post" action="/tambah"> {{ csrf_field() }} <div class="form-group"> <label>Nama</label> <input type="text" name="nama" class="form-control" placeholder="Bagus Rizkita"> @if($errors->has('nama')) <div class="text-danger"> {{ $errors->first('nama')}} </div> @endif </div> <div class="form-group"> <label>No Tlp</label> <input type="text" name="tlp" class="form-control" placeholder="8955xxxx"> @if($errors->has('tlp')) <div class="text-danger"> {{ $errors->first('tlp')}} </div> @endif </div> <div class="form-group"> <label>Alamat</label> <textarea name="alamat" class="form-control" placeholder="Bareng Krajan, Krian, Sidoarjo, Jawa Timur"></textarea> @if($errors->has('alamat')) <div class="text-danger"> {{ $errors->first('alamat')}} </div> @endif </div> </div> <div class="modal-footer"> <div class="form-group"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> <input type="submit" class="btn btn-success" value="Simpan"> </div> </form> </div> </div> </div> </div> </div> </div> <!-- Optional JavaScript --> <!-- jQuery first, then Popper.js, then Bootstrap JS --> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"> </script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"> </script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"> </script> </body> </html>
Code View Edit :
<!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <title>Data Siswa</title> </head> <body> <div class="container"> <div class="card"> <div class="card-header text-center"> <strong>Data Siswa</strong> </div> <div class="card-body"> <form method="POST" action="/update/{{$siswa->id}}"> {{ csrf_field() }} <div class="form-group"> <label>Nama</label> <input type="text" name="nama" class="form-control" value = " {{$siswa->nama}} "> @if($errors->has('nama')) <div class="text-danger"> {{ $errors->first('nama')}} </div> @endif </div> <div class="form-group"> <label>No Tlp</label> <input type="text" name="tlp" class="form-control" value = " {{$siswa->tlp }}"> @if($errors->has('tlp')) <div class="text-danger"> {{ $errors->first('tlp')}} </div> @endif </div> <div class="form-group"> <label>Alamat</label> <textarea name="alamat" class="form-control">{{ $siswa->alamat }}</textarea> @if($errors->has('alamat')) <div class="text-danger"> {{ $errors->first('alamat')}} </div> @endif </div> <div class="form-group"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> <input type="submit" class="btn btn-success" value="Simpan"> </div> </form> </div> </div> <!-- Modal --> <div class="modal fade" id="inputSiswaBaru" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLabel">Input Siswa Baru</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <div class="modal-body"> <form method="post" action="/tambah"> {{ csrf_field() }} <div class="form-group"> <label>Nama</label> <input type="text" name="nama" class="form-control" value=" {{ $siswa->nama }} "> @if($errors->has('nama')) <div class="text-danger"> {{ $errors->first('nama')}} </div> @endif </div> <div class="form-group"> <label>No Tlp</label> <input type="text" name="tlp" class="form-control" placeholder="8955xxxx"> @if($errors->has('tlp')) <div class="text-danger"> {{ $errors->first('tlp')}} </div> @endif </div> <div class="form-group"> <label>Alamat</label> <textarea name="alamat" class="form-control" placeholder="Bareng Krajan, Krian, Sidoarjo, Jawa Timur"></textarea> @if($errors->has('alamat')) <div class="text-danger"> {{ $errors->first('alamat')}} </div> @endif </div> </div> <div class="modal-footer"> <div class="form-group"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> <input type="submit" class="btn btn-success" value="Simpan"> </div> </form> </div> </div> </div> </div> </div> </div> <!-- Optional JavaScript --> <!-- jQuery first, then Popper.js, then Bootstrap JS --> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"> </script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"> </script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"> </script> </body> </html>
Code SiswaController :
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use App\Siswa; class SiswaController extends Controller { public function index() { $siswa = Siswa::paginate(10); Return view('siswa.view', ['siswa'=>$siswa]); } public function tambah(Request $request) { $this->validate($request,[ 'nama' => 'required', 'tlp' => 'required', 'alamat' => 'required' ]); Siswa::create([ 'nama' => $request->nama, 'tlp' => $request->tlp, 'alamat' => $request->alamat ]); return redirect('/'); } public function hapus($id) { $siswa = Siswa::find($id); $siswa->delete(); return redirect('/'); } public function edit($id) { $siswa = Siswa::find($id); return view('siswa.edit', ['siswa'=>$siswa]); } public function update($id, Request $request) { $this->validate($request,[ 'nama' => 'required', 'alamat' => 'required' ]); $siswa = Siswa::find($id); $siswa->nama = $request->nama; $siswa->tlp = $request->tlp; $siswa->alamat = $request->alamat; $siswa->save(); return redirect('/'); } public function cari(Request $request) { $cari = $request->cari; $siswa = DB::table('siswa') ->where('nama','like',"%".$cari."%") ->orWhere('tlp','like',"%".$cari."%") ->orWhere('alamat','like',"%".$cari."%") ->orderBy('nama') ->paginate(); return view('siswa.view', ['siswa'=>$siswa]); } }