kali ini saya akan memberi tutorial CRUD menggunakan
Laravel.
1.
Petama kita jalankan terlebih dahulu laravelnya.
2.
Jika sudah, buka file .env lalu ubah database
sesuai dengan yang anda gunakan.
3.
Lalu ketik perintah seperti dibawah ini,
kemudian enter.
4.
Jika sudah selesai, ketikan perintah seperti
dibawah ini untuk membuat file controller nya.
5.
Lalu ketik lagi perintah seperti dibawah ini
untuk membuat model nya.
6.
Setelah itu buka file yang telah kita migrate
tadi di database>migrations.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateGurusTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('gurus',
function (Blueprint $table) {
$table->integer('id_guru', true);
$table->string('nama_guru',
50);
$table->integer('nip')->index();
$table->string('password',
70);
$table->string('tempat_lahir',
20);
$table->date('tanggal_lahir');
$table->text('alamat',
65535);
$table->string('no_telp',
14);
$table->string('level')->comment('1.
kepsek, waka 2. walas 3. kaprog 4. guru');
$table->string('email',
40);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('gurus');
}
}
|
7.
Setelah itu, kembali kita migrate dengan cara
sama seperti tadi.
8.
Setelah itu buat file master.blade.php di
resources>views.
<!doctype html>
<html lang="{{ config('app.locale') }}">
<head>
<meta
charset="utf-8">
<meta
http-equiv="X-UA-Compatible" content="IE=edge">
<meta
name="viewport" content="width=device-width,
initial-scale=1">
<title>Indhira
Sasadhara</title>
<!-- Fonts -->
<link
href="{{asset('css/app.css')}}" rel="stylesheet"
type="text/css">
</head>
<body>
<br><br>
@yield('content')
</body>
</html>
|
9.
Lalu buat folder “guru” di dalam folder views.
Kemudian buat file create.blade.php.
<!-- create.blade.php -->
@extends('master')
@section('content')
<center><label><h2>Tambah Data
Guru</h2</label></center>
<div class="container">
<form
method="post" action="{{url('guru')}}">
<div
class="form-group row">
{{csrf_field()}}
<label
for="lgFormGroupInput" class="col-sm-2 col-form-label
col-form-label-lg">Nama Guru</label>
<div
class="col-sm-10">
<input
type="text" class="form-control form-control-lg"
id="lgFormGroupInput" placeholder="Nama Guru"
name="nama_guru">
</div>
<label
for="lgFormGroupInput" class="col-sm-2 col-form-label
col-form-label-lg">NIP</label>
<div
class="col-sm-10">
<input
type="text" class="form-control form-control-lg"
id="lgFormGroupInput" placeholder="NIP"
name="nip">
</div>
<label
for="lgFormGroupInput" class="col-sm-2 col-form-label
col-form-label-lg">Password</label>
<div
class="col-sm-10">
<input
type="password" class="form-control form-control-lg"
id="lgFormGroupInput" placeholder="Password"
name="password">
</div>
<label
for="lgFormGroupInput" class="col-sm-2 col-form-label
col-form-label-lg">Tempat Lahir</label>
<div
class="col-sm-10">
<input
type="text" class="form-control form-control-lg"
id="lgFormGroupInput" placeholder="Tempat Lahir"
name="tempat_lahir">
</div>
<label
for="lgFormGroupInput" class="col-sm-2 col-form-label
col-form-label-lg">Tanggal Lahir</label>
<div
class="col-sm-10">
<input type="text"
class="form-control form-control-lg"
id="lgFormGroupInput" placeholder="Tanggal Lahir"
name="tanggal_lahir">
</div>
<label
for="lgFormGroupInput" class="col-sm-2 col-form-label
col-form-label-lg">No Telp</label>
<div
class="col-sm-10">
<input
type="text" class="form-control form-control-lg"
id="lgFormGroupInput" placeholder="No Telp"
name="no_telp">
</div>
<label
for="lgFormGroupInput" class="col-sm-2 col-form-label
col-form-label-lg">Email</label>
<div class="col-sm-10">
<input
type="text" class="form-control form-control-lg"
id="lgFormGroupInput" placeholder="Email"
name="email">
</div>
<label
for="lgFormGroupInput" class="col-sm-2 col-form-label
col-form-label-lg">Level</label>
<div
class="col-sm-10">
<input
type="text" class="form-control form-control-lg"
id="lgFormGroupInput" placeholder="Level"
name="level">
</div>
</div>
<div
class="form-group row">
<label
for="smFormGroupInput" class="col-sm-2 col-form-label
col-form-label-sm">Alamat</label>
<div
class="col-sm-10">
<textarea
name="alamat" rows="8"
cols="80"></textarea>
</div>
</div>
<div
class="form-group row">
<div
class="col-md-2"></div>
<input
type="submit" class="btn btn-primary">
</div>
</form>
</div>
@endsection
|
10.
Lalu buka file web.php di folder routes untuk
mengkoneksikan controllers nya.
<?php
Route::get('/', function () {
return view('welcome');
});
Route::resource('guru', 'guruController');
Auth::routes();
|
11.
Kemudian buka file guruController.php di folder
app>Http>Controllers.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\guru;
class guruController extends Controller
{
function index() {
$data['guru'] =
guru::all();
return
view('guru.index',$data);
}
public function create()
{
return
view('guru.create');
}
public function
store(Request $request)
{
$guru = new guru([
'id_guru' => $request->get('id_guru'),
'nama_guru' => $request->get('nama_guru'),
'nip' =>
$request->get('nip'),
'password' => $request->get('password'),
'tempat_lahir'
=> $request->get('tempat_lahir'),
'tanggal_lahir'
=> $request->get('tanggal_lahir'),
'alamat' => $request->get('alamat'),
'no_telp' => $request->get('no_telp'),
'level' => $request->get('level'),
'email' =>
$request->get('email'),
'created_at' =>
$request->get('created_at'),
'created_by' =>
$request->get('created_by')
]);
$guru->save();
return
redirect('/guru');
}
/**
* Display the specified
resource.
*
* @param int
$id
* @return
\Illuminate\Http\Response
*/
public function
show($id)
{
//
}
/**
* Show the form for
editing the specified resource.
*
* @param int
$id
* @return
\Illuminate\Http\Response
*/
public function
edit($id_guru)
{
$guru =
guru::find($id_guru);
return
view('guru.edit', compact('guru','id_guru'));
}
/**
* Update the specified
resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int
$id
* @return
\Illuminate\Http\Response
*/
public function
update(Request $request, $id)
{
$guru =
guru::find($id);
$guru->nama_guru =
$request->get('nama_guru');
$guru->nip = $request->get('nip');
$guru->password =
$request->get('password');
$guru->tempat_lahir = $request->get('tempat_lahir');
$guru->tanggal_lahir = $request->get('tanggal_lahir');
$guru->alamat =
$request->get('alamat');
$guru->no_telp =
$request->get('no_telp');
$guru->level =
$request->get('level');
$guru->email =
$request->get('email');
$guru->updated_at =
$request->get('updated_at');
$guru->created_at =
$request->get('created_at');
$guru->save();
return
redirect('/guru');
}
/**
* Remove the specified
resource from storage.
*
* @param int
$id
* @return
\Illuminate\Http\Response
*/
public function
destroy($id)
{
$guru =
guru::find($id);
$guru->delete();
return
redirect('/guru');
}
}
|
12.
Buka file guru.php di app isi seusai dengan
struktur table yang kamu gunakan.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class guru extends Model
{
protected $table =
"guru";
protected $primaryKey =
"id_guru";
protected $fillable =
['nama_guru','nip','password','tempat_lahir','tanggal_lahir','alamat','no_telp','level',
'email'];
}
|
13.
Lalu buka file index.blade.php di folder
resources>views>guru untuk mebuat tampilan awalnya.
<!-- index.blade.php -->
@extends('master')
@section('content')
<div
class="container-fluid">
<center><label><h1>Portal Administrasi
Guru</h1</label></center>
<br>
<a
href="{{url('/guru/create')}}" class="btn
btn-primary">Tambah Guru</a>
<br>
<br>
<table class="table
table-striped table-responsive table-bordered">
<thead>
<tr>
<th>NIP</th>
<th>Nama
Guru</th>
<th>Edit</th>
<th>Hapus</th>
</tr>
</thead>
<tbody>
@foreach($guru as $post)
<tr>
<td>{{$post['nip']}}</td>
<td>{{$post['nama_guru']}}</td>
<td><a
href="{{action('guruController@edit', $post['id_guru'])}}"
class="btn btn-success btn-block">Edit</a></td>
<td>
<form
action="{{action('guruController@destroy', $post['id_guru'])}}"
method="post">
{{csrf_field()}}
<input
name="_method" type="hidden" value="DELETE">
<button
class="btn btn-danger btn-block"
type="submit">Delete</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
@endsection
|
14.
Kemudian buat file edit.blade.php di folder
resources>views>guru.
<!-- edit.blade.php -->
@extends('master')
@section('content')
<center><label><h2>Edit Data
Guru</h2</label></center>
<div class="container">
<form
method="post" action="{{action('guruController@update',
$id_guru)}}">
<div
class="form-group row">
{{csrf_field()}}
<input
name="_method" type="hidden" value="PATCH">
<label
for="lgFormGroupInput" class="col-sm-2 col-form-label
col-form-label-lg">Nama Guru</label>
<div
class="col-sm-10">
<input
type="text" class="form-control form-control-lg"
id="lgFormGroupInput" value="{{$guru->nama_guru}}"
name="nama_guru">
</div>
<label
for="lgFormGroupInput" class="col-sm-2 col-form-label
col-form-label-lg">NIP</label>
<div
class="col-sm-10">
<input
type="text" class="form-control form-control-lg"
value="{{$guru->nip}}" id="lgFormGroupInput"
placeholder="NIP" name="nip">
</div>
<label
for="lgFormGroupInput" class="col-sm-2 col-form-label
col-form-label-lg">Password</label>
<div
class="col-sm-10">
<input
type="password" class="form-control form-control-lg"
id="lgFormGroupInput" placeholder="Password"
name="password" value="{{$guru->password}}">
</div>
<label
for="lgFormGroupInput" class="col-sm-2 col-form-label
col-form-label-lg">Tempat Lahir</label>
<div
class="col-sm-10">
<input
type="text" class="form-control form-control-lg"
id="lgFormGroupInput" placeholder="Tempat Lahir"
name="tempat_lahir"
value="{{$guru->tempat_lahir}}">
</div>
<label
for="lgFormGroupInput" class="col-sm-2 col-form-label
col-form-label-lg">Tanggal Lahir</label>
<div
class="col-sm-10">
<input
type="text" class="form-control form-control-lg"
id="lgFormGroupInput" placeholder="Tanggal Lahir"
name="tanggal_lahir"
value="{{$guru->tanggal_lahir}}">
</div>
<label
for="lgFormGroupInput" class="col-sm-2 col-form-label
col-form-label-lg">No Telp</label>
<div
class="col-sm-10">
<input
type="text" class="form-control form-control-lg"
id="lgFormGroupInput" placeholder="No Telp"
name="no_telp" value="{{$guru->no_telp}}">
</div>
<label
for="lgFormGroupInput" class="col-sm-2 col-form-label
col-form-label-lg">Email</label>
<div
class="col-sm-10">
<input
type="text" class="form-control form-control-lg"
id="lgFormGroupInput" placeholder="Email"
name="email" value="{{$guru->email}}">
</div>
<label
for="lgFormGroupInput" class="col-sm-2 col-form-label
col-form-label-lg">Level</label>
<div
class="col-sm-10">
<input
type="text" class="form-control form-control-lg"
id="lgFormGroupInput" placeholder="Level"
name="level" value="{{$guru->level}}">
</div>
</div>
<div
class="form-group row">
<label
for="smFormGroupInput" class="col-sm-2 col-form-label
col-form-label-sm">Alamat</label>
<div
class="col-sm-10">
<textarea
name="alamat" rows="8"
cols="80">{{$guru->alamat}}</textarea>
</div>
</div>
<div
class="form-group row">
<div
class="col-md-2"></div>
<input
type="submit" value="update" class="btn
btn-primary">
</div>
</form>
</div>
@endsection
|
15.
Lalu nyalakan xampp terlebih dahulu untuk
mengecek tampilannya kemudian ketik url “ http://localhost:8000/guru
“
16.
Klik Tambah guru untuk menambah data guru.
17.
Klik edit untuk mengubah data guru. Disini saya
akan mengubah nama guru “indhira” menjadi “sasadhara”.
18.
Untuk menghapus klik tombol delete. Saya akan
menghapus data guru “sasadhara”.
Data berhasil dihapus :
19.
selesaiiiii
Tidak ada komentar:
Posting Komentar