1. Mengubah dari db facade ke ORM untuk tabel t_kelas
(Create, Edit,Delete)
2. Membuat tabel guru field (nip, nama_guru, jenis_kelamin,
alamat) , buat migrasi, view CRUD nya dan gunakan metode ORM untuk insert dan
update untuk tabel guru
3. Perbedaan dari penggunaan DB Facades dan Eloquent?
Jawab
Studi kasus 1
1. Membuat model baru Kelas.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Kelas extends Model
{
public $table = 't_kelas';
protected $fillable = ['id','nama_kelas','jurusan','lokasi_ruangan','nama_wali_kelas'];
}
?>
2. Modifikasi controller kelas bagian index
buka file app/Http/Controllers/KelasController.php
public function index()
{
//$data['kelas'] = \DB::table('t_kelas')
//->orderBy('nama_kelas')
//->orderBy('lokasi_ruangan')
//->where('nama_wali_kelas','like','A%')
//->orderBy('jurusan','nama_kelas')
//->where('jurusan','=', 'Audio Video')
//->get();
$data ['kelas'] = \App\Kelas::orderBy('nama_kelas')->get();
return view('kelas',$data);
}
3. Modifikasi controller kelas bagian store
buka file app/Http/Controllers/KelasController.php
public function store(Request $request){
$rule=[
'nama_kelas' => 'required|bail',
'jurusan' => 'required',
'lokasi_ruangan' => 'required',
'nama_wali_kelas' => 'required',
];
$this->validate($request, $rule);
$input = $request->all();
//unset($input['_token']);
//$status = \DB::table('t_kelas')->insert($input);
$status = \App\Kelas::create($input);
if($status){
return redirect('/kelas')->with('success','Data berhasil ditambahkan !!!');
} else {
return redirect('/kelas/create')->with('error','Data gagal ditambahkan !!!');
}
}
4. Modifikasi controller kelas bagian update
buka file app/Http/Controllers/KelasController.php
public function update(Request $request, $id){
$rule=[
'nama_kelas' => 'required',
'jurusan' => 'required',
'lokasi_ruangan' => 'required',
'nama_wali_kelas' => 'required',
];
$this->validate($request, $rule);
$input = $request->all();
//unset($input['_token']);
//unset($input['_method']);
//$status = \DB::table('t_kelas')->where('id',$id)->update($input);
$kelas = \App\Kelas::find($id);
$status = $kelas->update($input);
if($status){
return redirect('/kelas')->with('success','Data berhasil diubah !!!');
} else {
return redirect('/kelas/create')->with('error','Data gagal diubah !!!');
}
}
5. Modifikasi controller kelas bagian destroy
buka file app/Http/Controllers/KelasController.php
public function destroy(Request $request, $id){
//$status = \DB::table('t_kelas')->where('id',$id)->delete();
$kelas = \App\Kelas::find($id);
$status = $kelas->delete();
if($status){
return redirect('/kelas')->with('success','Data berhasil di Hapus !!!');
} else {
return redirect('/kelas/create')->with('error','Data gagal dihapus !!!');
}
}
6. Menambahkan fungsi Eloquent untuk tambah data
buka file app/Http/Controllers/KelasController.php lalu
ubah/tambahkan
public function store(Request $request){
$rule=[
'nama_kelas' => 'required|bail',
'jurusan' => 'required',
'lokasi_ruangan' => 'required',
'nama_wali_kelas' => 'required',
];
$this->validate($request, $rule);
$input = $request->all();
//unset($input['_token']);
//$status = \DB::table('t_kelas')->insert($input);
//$status = \App\Kelas::create($input);
$kelas = new \App\Kelas;
$kelas->nama_kelas = $input['nama_kelas'];
$kelas->jurusan = $input['jurusan'];
$kelas->lokasi_ruangan = $input['lokasi_ruangan'];
$kelas->nama_wali_kelas = $input['nama_wali_kelas'];
$status = $kelas->save();
if($status){
return redirect('/kelas')->with('success','Data berhasil ditambahkan !!!');
} else {
return redirect('/kelas/create')->with('error','Data gagal ditambahkan !!!');
}
}
7. Menambahkan fungsi
Eloquent untuk edit data
buka file app/Http/Controllers/KelasController.php lalu
ubah/tambahkan
public function update(Request $request, $id){
$rule=[
'nis' => 'required|numeric|digits:10',
'nama_lengkap' => 'required|string',
'jenkel' => 'required',
'goldar' => 'required',
];
$this->validate($request, $rule);
$input = $request->all();
//unset($input['_token']);
//unset($input['_method']);
//$status = \DB::table('t_siswa')->where('id',$id)->update($input);
$siswa = \App\Siswa::find($id);
//$status = $siswa->update($input);
$siswa->nis = $input['nis'];
$siswa->nama_lengkap = $input['nama_lengkap'];
$siswa->jenkel = $input['jenkel'];
$siswa->goldar = $input['goldar'];
$status = $siswa->update();
if($status){
return redirect('/siswa')->with('success','Data berhasil diubah !!!');
} else {
return redirect('/siswa/create')->with('error','Data gagal diubah !!!');
}
}
8. Cobalah kalian run apa berhasil atau tidak, Tambah Data dan juga Edit Data serta Delete Data
Studi kasus 2
1. Buat migration table guru di cmd dengan mengetik php
artisan make:migration create_t_guru kemudian akan muncul ini jika berhasil:
D:\xampp2\htdocs\laravel5.7-fresh>php artisan make:migration create_t_guru
Created Migration: 2019_03_10_104631_create_t_guru
2. Edit file create table guru ini dia isinya
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTGuru extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('t_guru', function (Blueprint $table){
$table->increments('id');
$table->bigInteger('nip');
$table->string('nama_guru', 100);
$table->string('jenis_kelamin', 1);
$table->string('alamat', 100);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
3. Balik lagi ke cmd guys lalu ketikan php artisan migrate,
jika berhasil maka akan muncul
D:\xampp2\htdocs\laravel5.7-fresh>php artisan migrate
Migrating: 2019_03_10_104631_create_t_guru
Migrated: 2019_03_10_104631_create_t_guru
4. Lihat apakah berhasil atau tidak dengan membuka di phpMyAdmin
5. Buatlah Controller untuk table guru * saya coba buat
manual, tidak menggunakan cmd ya guys ini dia (sudah dilengkapi dengan operasi
crud nya)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class GuruController extends Controller
{
public function index()
{
//$data['guru'] = \DB::table('t_guru')
//->orderBy('nip')
//->orderBy('jenis_kelamin')
//->where('nama_guru','like','%R%')
//->get();
$data ['guru'] = \App\Guru::orderBy('jenis_kelamin')->get();
return view('guru',$data);
}
//create
public function create(){
return view('guru.form');
}
//store
public function store(Request $request){
$rule=[
'nip' => 'required',
'nama_guru' => 'required|string',
'jenis_kelamin' => 'required',
'alamat' => 'required'
];
$this->validate($request, $rule);
$input = $request->all();
//unset($input['_token']);
//$status = \DB::table('t_guru')->insert($input);
//$status = \App\Guru::create($input);
$guru = new \App\Guru;
$guru->nip = $input['nip'];
$guru->nama_guru = $input['nama_guru'];
$guru->jenis_kelamin = $input['jenis_kelamin'];
$guru->alamat = $input['alamat'];
$status = $guru->save();
if($status){
return redirect('/guru')->with('success','Data berhasil ditambahkan !!!');
} else {
return redirect('/guru/create')->with('error','Data gagal ditambahkan !!!');
}
}
// EDIT
public function edit (Request $request, $id){
$data['guru'] = \DB::table('t_guru')->find($id);
return view('guru.form',$data);
}
// Update
public function update(Request $request, $id){
$rule=[
'nip' => 'required',
'nama_guru' => 'required',
'jenis_kelamin' => 'required',
'alamat' => 'required'
];
$this->validate($request, $rule);
$input = $request->all();
//unset($input['_token']);
//unset($input['_method']);
//$status = \DB::table('t_guru')->where('id',$id)->update($input);
$guru = \App\Guru::find($id);
//$status = $siswa->update($input);
$guru->nip = $input['nip'];
$guru->nama_guru = $input['nama_guru'];
$guru->jenis_kelamin = $input['jenis_kelamin'];
$guru->alamat = $input['alamat'];
$status = $guru->update();
if($status){
return redirect('/guru')->with('success','Data berhasil diubah !!!');
} else {
return redirect('/guru/create')->with('error','Data gagal diubah !!!');
}
}
// DELETE
public function destroy(Request $request, $id){
//$status = \DB::table('t_guru')->where('id',$id)->delete();
$guru =\App\Guru::find($id);
$status = $guru->delete();
if($status){
return redirect('/guru')->with('success','Data berhasil di Hapus !!!');
} else {
return redirect('/guru/create')->with('error','Data gagal dihapus !!!');
}
}
}
6. Route dari table guru
Route::get('/guru','GuruController@index');
Route::get('/guru/create','GuruController@create');
Route::post('/guru','GuruController@store');
Route::get('/guru/{id}/edit','GuruController@edit');
Route::patch('/guru/{id}','GuruController@update');
Route::delete('/guru/{id}','GuruController@destroy');
7. Model ORM table guru
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Guru extends Model
{
public $table = 't_guru';
protected $fillable = ['id','nip','nama_guru','jenis_kelamin','alamat'];
}
?>
8. Membuat view table guru
@extends('layouts.app')
@section('content')
<!--ALERT-->
<div class="container">
<div class="row">
@if(session('success'))
<div class="alert alert-success">
{{ session ('success') }}
</div>
@endif
@if(session('error'))
<div class="alert alert-error">
{{ session ('error') }}
</div>
@endif
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-10">
<h3>Biodata Guru</h3>
</div>
<div class="col-sm-2">
<a class="btn btn-sm btn-success" href="{{ url('/guru/create') }}">+</a>
<a class="btn btn-sm btn-outline-primary" href="{{ url('/kelas') }}">Kelas</a>
<a class="btn btn-sm btn-outline-primary" href="{{ url('/siswa') }}">Siswa</a>
</div>
<table class="table table-bordered table-hover">
<tr>
<th width = "50px"><b>No.</b></th>
<th width = "200px">Nip</th>
<th width = "300px">Nama Lengkap</th>
<th width = "180px">Jenis Kelamin</th>
<th width = "300px">Alamat</th>
<th colspan="2" width = "auto">Option</th>
</tr>
@foreach ($guru as $teacher)
<tr>
<td>{{ isset($i) ? ++$i : $i = 1 }}</td>
<td>{{$teacher->nip}}</td>
<td>{{$teacher->nama_guru}}</td>
<td>{{$teacher->jenis_kelamin}}</td>
<td>{{$teacher->alamat}}</td>
<td headers="">
<a href="{{ url('/guru/' . $teacher->id.'/edit') }}" class="btn btn-sm btn-primary">Edit</a>
</td>
<td>
<form action ="{{ url('/guru/' . $teacher->id) }}" method = "POST">
@method('DELETE')
@csrf
<button type="submit" class="btn btn-sm btn-danger">Delete</button>
</form>
</td>
</tr>
@endforeach
</table>
</div>
@endsection
9. Buat view form tambah data guru
@extends('layouts.app')
@section('content')
<div class="container">
@if(session('error'))
<div class="alert alert-error">
{{ session('error') }}
</div>
@endif
@if(count($errors) > 0)
<div class="alert alert-danger">
<strong>Warning !!!</strong>
<br>
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
</div>
<form action="{{url('guru', @$guru->id) }}" method="POST">
@csrf
@if(!empty($guru))
@method('PATCH')
@endif
<div class="container">
<h3>Data Guru</h3>
<div class="row">
<div class="col-md-12">
<strong>NIP :</strong>
<input type="text" name="nip" class="form-control" placeholder="NIP" value="{{ old('nip', @$guru->nip) }}">
</div>
<div class="col-md-12">
<strong>Nama Guru :</strong>
<input type="text" name="nama_guru" class="form-control" placeholder="Nama Guru" value="{{ old('nama_guru', @$guru->nama_guru) }}">
</div>
<div class="col-md-12">
<strong>Alamat :</strong>
<input type="text" name="alamat" class="form-control" placeholder="Alamat" value="{{ old('alamat', @$guru->alamat) }}">
</div>
<div class="col-md-12">
<b><label for="jenkel">Jenis Kelamin :</label></b>
<div class="radio">
<label>
<input type="radio" name="jenis_kelamin" id="L" value="L" {{old('jenis_kelamin', @$guru->jenis_kelamin) == 'L' ? 'checked ' : ''}}>
Laki-laki
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="jenis_kelamin" id="P" value="P" {{old('jenis_kelamin', @$guru->jenis_kelamin) == 'P' ? 'checked ' : ''}}>
Perempuan
</label>
</div>
</div>
<div class="col-md-12">
<button type="submit" class="btn btn-block btn-primary" value="Simpan">Submit</button>
<a href="{{url('/guru')}}" class="btn btn-block btn-danger">Back</a>
</div>
</div>
</form>
</div>
@endsection
10. Sekarang akses di Browser kalian
Studi kasus 3
Perbedaannya
Eloquent (ORM)
$siswa = \App\Siswa::find($id); $status = $siswa->update();
DB Facade
$status = \DB::table('t_siswa')->where('id',$id)->update($input);
- DB Facades berinteraksi langsung ke Database, layaknya Query. Itulah kenapa DB Facades biasa disebut Query Builder
- Eloquent lebih ke menyambungkan model dengan Database, itulah kenapa sebelum menjalankan Eloquent, Admin membuat model Siswa.php didalam \App\
- Jika DB Facades lebih mirip Query maka Eloquent bisa disamakan dengan ORM alias Object Relational Mapping. Untuk artikel mengenai ORM bisa dilihat disini
- Dari segi syntax Eloquent ORM lebih mudah dipahami dan juga lebih singkat daripada db Facade, juga tidak perlu menggunakan query seperti where,get
- Dari segi keamanan, lebih aman db facade karena mengunakan pdo parameter untuk melindungi dari serangan injeksi sql
Comments
Post a Comment