Simple Laravel Step 3 (CRUD Murid)

-
Assalamu 'alaikum wr wb
Saya akan menyambung postingan sebelumnya membuat proyek simpel dengan Framework Laravel,
Sebelum dimulai mari kita awali dengan Bissmillah,,,,
-
buat tabel bernama murid dan isi fieldnya
-
-
oke langsung aja kita akan membuat controller

-
nama controller : MuridController
-
kita coba Read data dari database
-
tambahkan koding di app/Http/Controller/MuridController
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class MuridController extends Controller
{
    public function index() {
        //view data dari tabel murid
        $murid = DB::table('murid')->get();

        //mengurimkan ke view murid/index.blade.php
        return view('murid.index',['murid' => $murid]);
    }

}
-
tambahkan koding di routes/web.php
Route::get('/murid','MuridController@index');
-
buat tampilan view -> views/murid/index.blade.php
<!DOCTYPE html>
<html>
<head>
    <title>Halaman Murid</title>
</head>
<body>

    <h3>
        Data Murid
    </h3>
    <a href="#">Tambah murid</a><br/><br/>
    <table border="2">
        <tr>
            <th>Nim</th>
            <th>Nama Lengkap</th>
            <th>Pas Foto</th>
            <th>Aksi</th>
        </tr>
        @foreach($murid as $mrd)
        <tr>
            <td>{{ $mrd->id }}</td>
            <td>{{ $mrd->nama_murid }}</td>
            <td>{{ $mrd->foto_murid }}</td>
            <td>
                ubah | hapus
            </td>
        </tr>
        @endforeach
    </table>

</body>
</html>
-
jalankan http://localhost/magerblog/murid
hasilnyaa,,
 -
buat path untuk menyimpan foto yang akan disimpan
-
masukkan gambar apa saja beri nama sesuai yg ada di field foto_murid dalam database
-
Revisi pada tampilan -> views/murid/index.blade.php
<!DOCTYPE html>
<html>
<head>
    <title>Halaman Murid</title>
</head>
<body>

    <h3>
        Data Murid
    </h3>
    <a href="/magerblog/murid/tambah">Tambah murid</a><br/><br/>
    <table border="2">
        <tr>
            <th>Nim</th>
            <th>Nama Lengkap</th>
            <th>Pas Foto</th>
            <th>Aksi</th>
        </tr>
        @foreach($murid as $mrd)
        <tr>
            <td>{{ $mrd->id }}</td>
            <td>{{ $mrd->nama_murid }}</td>
            <td><img src="{{ asset('foto_murid/'.$mrd->foto_murid) }}" height="70"></td>
            <td>
                ubah | hapus
            </td>
        </tr>
        @endforeach
    </table>

</body>
</html>
-
reload tampilan ctrl+R
-
nahhh untuk read data sudah selesai
sekarang kita coba tambah dataa,
-
pada routes/web.php tambahkan koding

//master murid
Route::get('/murid/tambah','MuridController@tambah');
Route::post('/murid/store','MuridController@store');
-
pada controller app/Http/Controller/MuridController tambahkan

    public function store(Request $request) {
    // input data ke table mahasiswa 'mahasiswa_nama' = field di database mhs_nama = name dr tambah.blade.php  
    $file = $request->file('foto_mrd');
    $path = 'foto_murid/';
    $foto = $request->nama_mrd.'.'.$file->getClientOriginalExtension();
  
    $file->move($path, $foto);
    DB::table('murid')->insert([
        'id' => $request->nim_mrd,
        'nama_murid' => $request->nama_mrd,
        'foto_murid' => $foto
      
    ]);

    // setelah proses selesai alihkan ke halaman mahasiswa
    return redirect('/murid');
    }
-
lalu buat file tambah.blade.php di dalam views/murid
<!DOCTYPE html>
<html>
<head>
    <title>Tambah murid</title>
</head>
<body>

   <h3>
        Tambah Mahasiswa
    </h3>
    <a href="/magerblog/murid">Kembali</a>
    <br/>
    <br/>
    <form action="/magerblog/murid/store" method="post" enctype="multipart/form-data">
    {{ csrf_field() }}
    Nim <br/>
    <input type="text" name="nim_mrd" required="required" placeholder="Nomor Induk Murid"><br>
    Nama Murid <br/>
    <input type="text" name="nama_mrd" required="required" placeholder="Nama Murid"><br>
    Foto <br/>
    <input type="file" name="foto_mrd" required="required" placeholder="Foto" accept="image/*"><br>
    <input type="submit" value="Simpan">

    </form>

</body>
</html>
-
demo http://localhost/magerblog/murid
sekarang sudah bisa dilihat dan disimpan,,
nahh sekarang kita akan buat fungsi edit dan hapuss,,
-
tambahkan pada routers/web.php
//master murid
Route::get('/murid','MuridController@index');
Route::get('/murid/tambah','MuridController@tambah');
Route::post('/murid/store','MuridController@store');
Route::get('/murid/edit/{id}','MuridController@edit');
Route::post('/murid/update','MuridController@update');
Route::get('/murid/hapus/{id}','MuridController@hapus');
-
revisi views -> murid/index.blade.php
<!DOCTYPE html>
<html>
<head>
    <title>Halaman Murid</title>
</head>
<body>

    <h3>
        Data Murid
    </h3>
    <a href="/magerblog/murid/tambah">Tambah murid</a><br/><br/>
    <table border="2">
        <tr>
            <th>Nim</th>
            <th>Nama Murid</th>
            <th>Foto Murid</th>

            <th>Aksi</th>
        </tr>
        @foreach($murid as $mrd)
        <tr>
            <td>{{ $mrd->id }}</td>
            <td>{{ $mrd->nama_murid }}</td>
            <td><img src="{{ asset('foto_murid/'.$mrd->foto_murid) }}" height="70"></td>
            <td>
                <a href="/magerblog/murid/edit/{{ $mrd->id_murid }}">Edit</a> ||
                <a href="/magerblog/murid/hapus/{{ $mrd->id_murid }}">Hapus</a>

            </td>
        </tr>
        @endforeach
    </table>

</body>
</html>
-
buat views edit -> murid/edit.blade.php 
<!DOCTYPE html>
<html>
<head>
    <title>edit</title>
</head>
<body>

    <h3>
        Edit Data Murid
    </h3>
    <a href="/magerblog/murid"> Kembali</a>
    @foreach($murid as $mrd)
    <form action="/magerblog/murid/update" method="post" enctype="multipart/form-data">
        {{ csrf_field() }}

        <input type="hidden" name="id" value="{{ $mrd->id_murid }}"> <br/>
        Nim <br/>
        <input type="text" required="required" name="nim_mrd" value="{{ $mrd->id }}"> <br/>
        Nama Murid <br/>
        <input type="text" required="required" name="nama_mrd" value="{{ $mrd->nama_murid }}"> <br/>
        Foto Murid<br/>
        <input type="file" required="required" name="foto_mrd" value="{{ $mrd->foto_murid }}" accept="image/*"> <br/>
        <input type="submit" value="Update Data">
    </form>
    @endforeach

</body>
</html>
-
tambahkan koding pada MuridController untuk aksi si edit dan hapus
    public function edit($id) {
    // mengambil data murid berdasarkan id yang dipilih
    $murid = DB::table('murid')->where('id_murid',$id)->get();
    // passing data mahasiswa yang didapat ke view edit.blade.php
    return view('murid.edit',['murid' => $murid]);
    }

    public function update(Request $request) {
    // update data murid
    $file = $request->file('foto_mrd');
    $path = 'foto_murid/';
    $foto = $request->nama_mrd.'.'.$file->getClientOriginalExtension();
  
    $file->move($path, $foto);
    DB::table('murid')->where('id_murid',$request->id)->update([
        'id' => $request->nim_mrd,
        'nama_murid' => $request->nama_mrd,
        'foto_murid' => $foto
    ]);
    // setelah proses selesai alihkan ke halaman master murid
    return redirect('/murid');
    }

    public function hapus($id) {
    // menghapus data murid berdasarkan id yang dipilih
    DB::table('murid')->where('id_murid',$id)->delete();
    
    // setelah proses selesai alihkan ke halaman master murid
    return redirect('/murid');
    }
-
tambahkan akses login,
jadi yang bisa input harus login dahuluu,,
pada controller, MuridController tambahkan koding ini diatas public function index()

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
-
selesaiii..
sekarang kita demoooo,,,
-
-
selesaiii tunggu post berikutnya yaaaa :D
post berikutnya insya Allah kita akan edit tampilang dengan bootstrap dan datatable pada master murid :D
Previous
Next Post »
Thanks for your comment