Untuk pembejalaran pembuat aplikasi CRUD (Create, Read, Update, Delete) di Laravel 5.5 kita akan membuat contoh database karyawan. Kita tidak akan mengunakan HTML/Form helper di Laravel.

Kebutuhan

– LAMP/LEMP yang sudah berjalan
– composer
– text editor (Sublime Text)
– koneksi internet

Buat Database

Saya menggunakan MySQL/MariaDB, login ke MySQL

mysql -u root -p

buat database baru dengan nama crudlaravel55

create database crudlaravel55;

Install Laravel

Install laravel dengan composer

composer create-project --prefer-dist laravel/laravel crudlaravel5

Edit file .env
Untuk koneksi ke database, ubah file .env

DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

contoh

DB_DATABASE=crudlaravel55
DB_USERNAME=crudlaravel55
DB_PASSWORD=crudlaravel55123

Membuat Tabel Database

Kita akan membuat satu tabel dengan nama karyawan

php artisan make:migration tabel_karyawan
## outputnya
Created Migration: 2017_11_10_033011_tabel_karyawan

Edit file di migration yang baru dibuat, letaknya ada di folder database/migrations/, di contoh nama filenya database/migrations/2017_11_10_033011_tabel_karyawan.php

Ubah menjadi

    public function up()
    {
        Schema::create('karyawan', function (Blueprint $table) {
            $table->increments('id');
            $table->string('nama_lengkap');
            $table->string('NIK');
            $table->string('Jabatan');
            $table->timestamps();
        });
    }
    public function down()
    {
        Schema::dropIfExists('karyawan');
    }

Buka file app/Providers/AppServiceProvider.php ubah function boot() menjadi

    public function boot()
    {
        \Schema::defaultStringLength(191);
    }

Sekarang jalankan php artisan migrate agar tabel tersebut dibuat di database

php artisan migrate

Routes

Buka file routes/web.php tambahkan

Route::resource('karyawan', 'KaryawanController');

Coding CRUD

Disini baru kita mulai proses kodingnya.

1. Model

Buat model untuk tabel karyawan yang tadi dibuat

php artisan make:model Karyawan

file model yang baru dibuat ada di app/Karyawan.php, ubah menjadi

class Karyawan extends Model
{
    protected $table = 'karyawan';
    protected $fillable = ['nama_lengkap', 'NIK', 'jabatan'];
}

2. Controller

Buat controller untuk Karyawan

php artisan make:controller KaryawanController

File controller tersebut berada di app/Http/Controllers/KaryawanController.php, isi lengkap file KaryawanController.php

<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
use App\Karyawan;
 
class KaryawanController extends Controller
{
    public function index() {
        $karyawan = Karyawan::all();
        return view('karyawan.index', compact('karyawan'));
    }
 
    public function create() {
        return view('karyawan.create');
    }
 
    public function edit($id) {
        $karyawan = Karyawan::findOrFail($id);
        return view('karyawan.edit', compact('karyawan'));
    }
 
    public function store(Request $request) {
        Karyawan::create($request->all());
        \Session::flash('notifikasi', 'Karyawan berhasil ditambah.');
        return redirect('karyawan');
    }
 
    public function update(Request $request, $id) {
        $karyawan = Karyawan::findOrFail($id);
        $karyawan->update($request->all());
        \Session::flash('notifikasi', 'Karyawan berhasil diubah.');
        return redirect('karyawan');
    }
 
    public function destroy($id) {
        Karyawan::destroy($id);
        \Session::flash('notifikasi', 'Karyawan berhasil dihapus.');
        return redirect('karyawan');
    }
}

3. View

Buat file resources/views/template.blade.php yang akan digunakan sebagai master template untuk web. Isi file template.blade.php

<!doctype html>
<html lang="en">
<head>
    <title>CRUD Laravel 5.5 - jaranguda.com</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
    <style type="text/css">
    body {
      padding-top: 2rem;
    }
    </style>
</head>
<body>
 
    <main role="main">
 
        <div class="container">
 
            <div class="row">
                <div class="col-md-12">
 
                @if (session('notifikasi'))
                    <div class="alert alert-success">
                        {{ session('notifikasi') }}
                    </div>
                @endif
 
                @yield('content')
 
                </div>
            </div>
        </div>
 
    </main>
 
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>
</body>
</html>

Untuk menampilkan data dari controller karyawan, kita akan membuat satu folder karyawan dan beberapa file. Buat folder karyawan di resources/views/.

Index dan Delete

isi file index.blade.php (resources/views/karyawan/index.blade.php)

@extends('template')
 
@section('content')
 
<h3>Data Karyawan</h3>
<hr>
Tambah Karyawan
<hr>
@if(count($karyawan) > 0)
 
    <table class="table">
        <thead>
            <tr>
                <th>Nama Lengkap</th>
                <th>NIK</th>
                <th>Jabatan</th>
                <th>#</th>
            </tr>
        </thead>
        <tbody>
            @foreach ($karyawan as $data)
            <tr>
                <td>{{ $data->nama_lengkap }}</td>
                <td>{{ $data->NIK}}</td>
                <td>{{ $data->jabatan }}</td>
                <td>
                    <a href="{{ URL::route('karyawan.edit',$data->id) }}" class="btn btn-info btn-sm"></i> Edit</a>
                    <form action="{{action('KaryawanController@destroy', $data->id)}}" method="post" class="hapus" style="display:inline">
                    {{csrf_field()}}
                    <input name="_method" type="hidden" value="DELETE">
                    <button class="btn btn-danger" type="submit">Delete</button>
                    </form>
                </td>
            </tr>
            @endforeach
        </tbody>
    </table>
@else
belum ada data
@endif
 
 
@stop

Create

isi file create.blade.php (resources/views/karyawan/create.blade.php)

@extends('template')
 
@section('content')
 
<h3>Data Karyawan</h3>
<hr>
 
    <form class="form-horizontal" role="form" method="POST" action="{{ url('/karyawan') }}">
        {{ csrf_field() }}
        <div class="form-group{{ $errors->has('nama_lengkap') ? ' has-error' : '' }}">
            <label for="nama_lengkap" class="col-md-4 control-label">Nama</label>
            <div class="col-md-6">
                <input id="nama_lengkap" type="text" class="form-control" name="nama_lengkap" value="{{ old('nama_lengkap') }}" required="required">
                @if ($errors->has('nama_lengkap'))
                <span class="help-block">
                    <strong>{{ $errors->first('nama_lengkap') }}</strong>
                </span>
                @endif
            </div>
        </div>
 
        <div class="form-group{{ $errors->has('NIK') ? ' has-error' : '' }}">
            <label for="NIK" class="col-md-4 control-label">NIK</label>
            <div class="col-md-6">
                <input id="NIK" type="text" class="form-control" name="NIK" value="{{ old('NIK') }}" required="required">
                @if ($errors->has('NIK'))
                <span class="help-block">
                    <strong>{{ $errors->first('NIK') }}</strong>
                </span>
                @endif
            </div>
        </div>
 
        <div class="form-group{{ $errors->has('jabatan') ? ' has-error' : '' }}">
            <label for="jabatan" class="col-md-4 control-label">Jabatan</label>
            <div class="col-md-6">
                <input id="jabatan" type="text" class="form-control" name="jabatan" value="{{ old('jabatan') }}" required="required">
                @if ($errors->has('jabatan'))
                <span class="help-block">
                    <strong>{{ $errors->first('jabatan') }}</strong>
                </span>
                @endif
            </div>
        </div>
 
 
        <div class="form-group">
            <div class="col-md-6 col-md-offset-4">
                <button type="submit" class="btn btn-primary">
                    <i class="fa fa-btn fa-user"></i> Simpan
                </button>
            </div>
        </div>
    </form>
 
@stop

Edit

isi file edit.blade.php (resources/views/karyawan/edit.blade.php)

@extends('template')
 
@section('content')
 
<h3>Data Karyawan</h3>
<hr>
 
@if(isset($karyawan))
        <form class="form-horizontal" role="form" method="POST" action="{{ url('/karyawan/' . $karyawan->id) }}">
        <input name="_method" type="hidden" value="PATCH">
        {{ csrf_field() }}
        <div class="form-group{{ $errors->has('nama_lengkap') ? ' has-error' : '' }}">
            <label for="nama_lengkap" class="col-md-4 control-label">Nama</label>
            <div class="col-md-6">
                <input id="nama_lengkap" type="text" class="form-control" name="nama_lengkap" value="{{ old('nama_lengkap', $karyawan->nama_lengkap) }}" required="required">
                @if ($errors->has('nama_lengkap'))
                <span class="help-block">
                    <strong>{{ $errors->first('nama_lengkap') }}</strong>
                </span>
                @endif
            </div>
        </div>
 
        <div class="form-group{{ $errors->has('NIK') ? ' has-error' : '' }}">
            <label for="NIK" class="col-md-4 control-label">NIK</label>
            <div class="col-md-6">
                <input id="NIK" type="text" class="form-control" name="NIK" value="{{ old('NIK', $karyawan->NIK) }}" required="required">
                @if ($errors->has('NIK'))
                <span class="help-block">
                    <strong>{{ $errors->first('NIK') }}</strong>
                </span>
                @endif
            </div>
        </div>
 
        <div class="form-group{{ $errors->has('jabatan') ? ' has-error' : '' }}">
            <label for="jabatan" class="col-md-4 control-label">Jabatan</label>
            <div class="col-md-6">
                <input id="jabatan" type="text" class="form-control" name="jabatan" value="{{ old('jabatan', $karyawan->Jabatan) }}" required="required">
                @if ($errors->has('jabatan'))
                <span class="help-block">
                    <strong>{{ $errors->first('jabatan') }}</strong>
                </span>
                @endif
            </div>
        </div>
 
        <div class="form-group">
            <div class="col-md-6 col-md-offset-4">
                <button type="submit" class="btn btn-primary">
                    <i class="fa fa-btn fa-user"></i> Simpan
                </button>
            </div>
        </div>
    </form>
 
@endif
 
 
@stop

Last Updated on 30 November 2017 by tommy

Join the Conversation

1 Comment

Your email address will not be published. Required fields are marked *