Crud Laravel 5 – Controller Resource

Ini adalah bagian keempat dari seri CRUD LARAVEL (simplecrud), pada bagian ini kita akan membuat proses untuk menyimpan, mengedit, mengupdate dan menghapus data kecamatan dan desa.

Pada bagian sebelumnya kita telah membuat file CrudKecamatan.php dan juga CrudDesa.php. Sebelum kita mengedit 2 file controller tersebut, kita akan membuat 2 buah folder di dalam folder views/admin, yaitu folder kecamatan dan desa. Pada masing-masing folder tersebut (kecamatan dan desa) buat 2 buah file.

  1. edit.blade.php
  2. index.blade.php

Kecamatan

Sekarang kita edit file controller CrudKecamatan.php

<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Kecamatan;

class CrudKecamatan extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $itemkecamatan = Kecamatan::get();
        $data = array('itemkecamatan' => $itemkecamatan);
        return view('admin.kecamatan.index',$data)->with('no',1);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return abort('404');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request,[
            'kode'=>'required',
            'nama'=>'required',
        ]);
        Kecamatan::create($request->all());
        return back()->with('success','Data berhasil disimpan');//kalo berhasil kembali ke halaman seblumnya dgn session success
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        return abort('404');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $itemkecamatan = Kecamatan::find($id);
        if (count($itemkecamatan) > 0) {
            $data = array('itemkecamatan' => $itemkecamatan);
            return view('admin.kecamatan.edit',$data);
        }else{
            return abort('404');
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request,[
            'kode'=>'required',
            'nama'=>'required',
        ]);
        Kecamatan::find($id)->update($request->all());
        return redirect()->route('kecamatan.index')->with('success','Data berhasil diupdate');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Kecamatan::find($id)->delete();
        return redirect()->route('kecamatan.index')->with('success','Data berhasil dihapus');
    }
}

Setelah selesai, kemudian edit file di dalam folder views/admin/kecamatan.

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-4 col-sm-4">
            <div class="panel panel-default">
              <div class="panel-heading">
                <h3 class="panel-title"><span class="glyphicon glyphicon-list-alt"></span> Form Data Kecamatan</h3>
              </div>
              <div class="panel-body">
                  @if(count($errors) > 0)
                    @foreach($errors->all() as $error)
                        <div class="alert alert-warning">{{ $error }}</div>
                    @endforeach
                  @endif
                  @if ($message = Session::get('error'))
                      <div class="alert alert-warning">
                          <p>{{ $message }}</p>
                      </div>
                  @endif
                  @if ($message = Session::get('success'))
                      <div class="alert alert-success">
                          <p>{{ $message }}</p>
                      </div>
                  @endif
                  {{ Form::open(array('route'=>'kecamatan.store','method'=>'post')) }}
                  <div class="form-group">
                    <label for="kode">Kode</label>
                    {{ Form::text('kode',null, array('class'=>'form-control')) }}
                  </div>
                  <div class="form-group">
                    <label for="nama">Nama</label>
                    {{ Form::text('nama',null, array('class'=>'form-control')) }}
                  </div>
                  <div class="form-group">
                    <button type="submit" class="btn btn-primary">Simpan</button>
                    <button type="reset" class="btn btn-warning">Reset</button>
                  </div>
                  {{ Form::close() }}
              </div>
            </div>
        </div>
        <div class="col-md-8 col-sm-8">
            <div class="panel panel-default">
              <div class="panel-heading">
                <h3 class="panel-title"><span class="glyphicon glyphicon-list"></span> Daftar Data Kecamatan</h3>
              </div>
              <div class="panel-body">
                  <div class="table-responsive">
                      <table class="table table-bordered">
                        <th>No</th>
                        <th>Kode</th>
                        <th>Nama</th>
                        <th></th>
                        @foreach($itemkecamatan as $kecamatan)
                        <tr>
                            <td>{{ $no++ }}</td>
                            <td>{{ $kecamatan->kode }}</td>
                            <td>{{ $kecamatan->nama }}</td>
                            <td>
                                <a href="{{ route('kecamatan.edit',$kecamatan->id) }}" class="btn btn-sm btn-primary"><span class="glyphicon glyphicon-edit"></span></a>
                                {!! Form::open(['method' => 'DELETE','route' => ['kecamatan.destroy', $kecamatan->id],'style'=>'display:inline']) !!}
                                <button type="submit" class="btn btn-sm btn-danger"><span class="glyphicon glyphicon-remove"></span></button>
                                {{ Form::close() }}
                            </td>
                        </tr>
                        @endforeach
                      </table>
                  </div>
              </div>
            </div>
        </div>
    </div>
</div>
@endsection
@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-4 col-sm-4">
            <div class="panel panel-default">
              <div class="panel-heading">
                <h3 class="panel-title"><span class="glyphicon glyphicon-list-alt"></span> Form Edit Data Kecamatan</h3>
              </div>
              <div class="panel-body">
                  @if(count($errors) > 0)
                    @foreach($errors->all() as $error)
                        <div class="alert alert-warning">{{ $error }}</div>
                    @endforeach
                  @endif
                  @if ($message = Session::get('error'))
                      <div class="alert alert-warning">
                          <p>{{ $message }}</p>
                      </div>
                  @endif
                  @if ($message = Session::get('success'))
                      <div class="alert alert-success">
                          <p>{{ $message }}</p>
                      </div>
                  @endif
                  {{ Form::model($itemkecamatan, array('route'=>array('kecamatan.update',$itemkecamatan->id),'method'=>'patch')) }}
                  <div class="form-group">
                    <label for="kode">Kode</label>
                    {{ Form::text('kode',null, array('class'=>'form-control')) }}
                  </div>
                  <div class="form-group">
                    <label for="nama">Nama</label>
                    {{ Form::text('nama',null, array('class'=>'form-control')) }}
                  </div>
                  <div class="form-group">
                    <button type="submit" class="btn btn-primary">Update</button>
                    <button type="reset" class="btn btn-warning">Reset</button>
                  </div>
                  {{ Form::close() }}
              </div>
            </div>
        </div>
    </div>
</div>
@endsection

Bagian data kecamatan telah selesai, silahkan lakukan proses menambahkan, mengedit, mengupate dan menghapus pada halaman form kecamatan.

Form Kecamatan

Desa

Langkahnya hampir sama seperti pada saat memproses CRUD data kecamatan, pertama edit controller CrudDesa.php

<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Desa;
use App\Kecamatan;

class CrudDesa extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $itemdesa = Desa::orderby('id','ASC')->with(['kecamatan'])->get();
        $itemkecamatan = array(''=>'Pilih Kecamatan');
        foreach (Kecamatan::orderBy('nama')->get() as $key) {
            $itemkecamatan[$key->id] = $key->kode." - ".$key->nama;
        }
        $data = array('itemdesa' => $itemdesa,
                    'itemkecamatan'=>$itemkecamatan);
        return view('admin.desa.index',$data)->with('no',1);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return abort('404');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request,[
            'kode'=>'required',
            'nama'=>'required',
            'kecamatan_id'=>'required'
        ]);
        Desa::create($request->all());
        return back()->with('success','Data berhasil disimpan');//kalo berhasil kembali ke halaman seblumnya dgn session success
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        return abort('404');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $itemdesa = Desa::find($id);
        if (count($itemdesa) > 0) {
            $itemkecamatan = array(''=>'Pilih Kecamatan');
            foreach (Kecamatan::orderBy('nama')->get() as $key) {
                $itemkecamatan[$key->id] = $key->kode." - ".$key->nama;
            }
            $data = array('itemdesa' => $itemdesa,
                        'itemkecamatan'=>$itemkecamatan);
            return view('admin.desa.edit',$data);
        }else{
            return abort('404');
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request,[
            'kode'=>'required',
            'nama'=>'required',
            'kecamatan_id'=>'required'
        ]);
        Desa::find($id)->update($request->all());
        return redirect()->route('desa.index')->with('success','Data berhasil diupdate');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Desa::find($id)->delete();
        return redirect()->route('desa.index')->with('success','Data berhasil dihapus');
    }
}

Terakhir edit 2 file di folder view/admin/desa.

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-4 col-sm-4">
            <div class="panel panel-default">
              <div class="panel-heading">
                <h3 class="panel-title"><span class="glyphicon glyphicon-list-alt"></span> Form Data Desa</h3>
              </div>
              <div class="panel-body">
                  {{ Form::open(array('route'=>'desa.store','method'=>'post')) }}
                  <div class="form-group">
                    <label for="kecamatan_id">Kecamatan</label>
                    {{ Form::select('kecamatan_id',$itemkecamatan,null, array('class'=>'form-control')) }}
                  </div>
                  <div class="form-group">
                    <label for="kode">Kode</label>
                    {{ Form::text('kode',null, array('class'=>'form-control')) }}
                  </div>
                  <div class="form-group">
                    <label for="nama">Nama</label>
                    {{ Form::text('nama',null, array('class'=>'form-control')) }}
                  </div>
                  <div class="form-group">
                    <button type="submit" class="btn btn-primary">Simpan</button>
                    <button type="reset" class="btn btn-warning">Reset</button>
                  </div>
                  {{ Form::close() }}
              </div>
            </div>
        </div>
        <div class="col-md-8 col-sm-8">
            <div class="panel panel-default">
              <div class="panel-heading">
                <h3 class="panel-title"><span class="glyphicon glyphicon-list"></span> Daftar Data Desa</h3>
              </div>
              <div class="panel-body">
                  <div class="table-responsive">
                      <table class="table table-bordered">
                        <th>No</th>
                        <th>Kecamatan</th>
                        <th>Kode</th>
                        <th>Nama</th>
                        <th></th>
                        @foreach($itemdesa as $desa)
                        <tr>
                            <td>{{ $no++ }}</td>
                            <td>{{ $desa->kecamatan->nama }}</td>
                            <td>{{ $desa->kode }}</td>
                            <td>{{ $desa->nama }}</td>
                            <td>
                                <a href="{{ route('desa.edit',$desa->id) }}" class="btn btn-sm btn-primary"><span class="glyphicon glyphicon-edit"></span></a>
                                {!! Form::open(['method' => 'DELETE','route' => ['desa.destroy', $desa->id],'style'=>'display:inline']) !!}
                                <button type="submit" class="btn btn-sm btn-danger"><span class="glyphicon glyphicon-remove"></span></button>
                                {{ Form::close() }}
                            </td>
                        </tr>
                        @endforeach
                      </table>
                  </div>
              </div>
            </div>
        </div>
    </div>
</div>
@endsection

 

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-4 col-sm-4">
            <div class="panel panel-default">
              <div class="panel-heading">
                <h3 class="panel-title"><span class="glyphicon glyphicon-list-alt"></span> Form Edit Data Desa</h3>
              </div>
              <div class="panel-body">
                  @if(count($errors) > 0)
                    @foreach($errors->all() as $error)
                        <div class="alert alert-warning">{{ $error }}</div>
                    @endforeach
                  @endif
                  @if ($message = Session::get('error'))
                      <div class="alert alert-warning">
                          <p>{{ $message }}</p>
                      </div>
                  @endif
                  @if ($message = Session::get('success'))
                      <div class="alert alert-success">
                          <p>{{ $message }}</p>
                      </div>
                  @endif
                  {{ Form::model($itemdesa, array('route'=>array('desa.update',$itemdesa->id),'method'=>'patch')) }}
                  <div class="form-group">
                    <label for="kecamatan_id">Kecamatan</label>
                    {{ Form::select('kecamatan_id',$itemkecamatan,null, array('class'=>'form-control')) }}
                  </div>
                  <div class="form-group">
                    <label for="kode">Kode</label>
                    {{ Form::text('kode',null, array('class'=>'form-control')) }}
                  </div>
                  <div class="form-group">
                    <label for="nama">Nama</label>
                    {{ Form::text('nama',null, array('class'=>'form-control')) }}
                  </div>
                  <div class="form-group">
                    <button type="submit" class="btn btn-primary">Update</button>
                    <button type="reset" class="btn btn-warning">Reset</button>
                  </div>
                  {{ Form::close() }}
              </div>
            </div>
        </div>
    </div>
</div>
@endsection

Sampai disini aplikasi simplecrud sudah bisa dijalankan. Langkah yang terakhir adalah bagaimana mendeploy atau menaruh file project simplecrud ke web hosting.

Selamat mencoba

CRUD LARAVEL (simplecrud)

  1. Create project dan install package
  2. Sistem otentikasi laravel 5 menggunakan zizaco/entrust
  3. Migration, model, controller dan template laravel 5
  4. Controller Resource
  5. Deploy Laravel ke Layanan Hosting

File project bisa dilihat diĀ https://github.com/fadlur/laravel-simplecrud

Tinggalkan Balasan

This site uses Akismet to reduce spam. Learn how your comment data is processed.