TUTORIAL CRUD LARAVEL DENGAN 2 TABEL BERELASI

TUTORIAL CRUD LARAVEL DENGAN 2 TABEL BERELASI


oke pada pembahasan kali ini saya akan membahas tentang CRUD ( Create Read Update Delete) pada laravel , sebelumnya kita sudah pernah membuat project pada pembahasan sebelumnya , jadi disini saya tidak akan membahas pembuatan projectnya.

saya membuat database tentang pembeli dan barang , saya ceritakan sedikit ehehe.. nah jadi nantinya si pembeli dapat membeli barang yang tersedia di sebuah toko maka dari itu pertama - tama kita buat dari barangnya terlebih dahulu.

1. Buat Database 
buat lah database dengan masuk ke browser lalu ketikan 

 localhost/phpmyadmin  

pilih new lalu ketikan nama database mu, saya menamain database saya 'latihan' lalu plih create .

3. Hubungkan Database ke project yang telah dibuat
buka file .env pada project kamu lalu ubah seperti dibawah ini

 DB_CONNECTION=mysql  
 DB_HOST=127.0.0.1  
 DB_PORT=3306  
 DB_DATABASE=latihan  
 DB_USERNAME=root  
 DB_PASSWORD=''  

2 . membuat tabel barang
buka cmd arahkan ke folder project kamu , lalu ketikan

 php artisan make:migration create_barangs_table  


3. buka folder project/database/migration disitu sudah ada file migration yang anda buat tadi untuk user dan password dihapus saja karena kita tidak akan menggunakannya.

lalu buka file cretae_barangs_table dan ubah seperti dibawah ini

 public function up()  
   {  
     Schema::create('barangs', function (Blueprint $table) {  
       $table->increments('id');  
       $table->string('nama');  
       $table->integer('stock');  
       $table->timestamps();  
     });  
   }  

itu berfungsi untuk membuat table database dengan nama 'barangs' yang isinya = id , nama , stock untuk created_at dan updated_at dibuat secara otomatis pada 'timestamps()'

jika sudah lalu ketikan pada cmd anda

 php artisan migrate  

jika mucul seperti tampilan diatas maka table barans berhasil dibuat.

4. membuat model barang dengan mengetikan

 php artisan make:model barang  

lalu buka file barang.php pada project/app/barang.php

lalu ketikan seperti di bawah ini

 <?php  
 namespace App;  
 use Illuminate\Database\Eloquent\Model;  
 class barang extends Model  
 {  
   protected $fillable = ['id','nama_barang','stock'];  
 }  

5. membuat controller barang
ketikan pada cmd

 php artisan make:controller BarangController --resource  

--resource berfungsi untuk membuat method seperti create , store ,edit ,update, delete secara otomatis

setelah file sudah dibuat lalu buka file BarangController pada project/app/http/controller/BarangController.php lalu ketikan seperti dibawah ini

 <?php  
 namespace App\Http\Controllers;  
 use Illuminate\Http\Request;  
 use App\barang;  
 class BarangController extends Controller  
 {  
   /**  
    * Display a listing of the resource.  
    *  
    * @return \Illuminate\Http\Response  
    */  
   public function index()  
   {  
     $data = barang::All();  
     return view('barang',compact('data'));  
   }  
   /**  
    * Show the form for creating a new resource.  
    *  
    * @return \Illuminate\Http\Response  
    */  
   public function create()  
   {  
     //  
   }  
   /**  
    * Store a newly created resource in storage.  
    *  
    * @param \Illuminate\Http\Request $request  
    * @return \Illuminate\Http\Response  
    */  
   public function store(Request $request)  
   {  
     $data = new barang();  
     $data->nama_barang = $request->nama_barang;  
     $data->stock = $request ->stock;  
     $data->save();  
     return redirect('/barang');  
   }  
   /**  
    * 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)  
   {  
     $barang = barang::find($id);  
     return view('formbarang',compact('barang'));  
   }  
   /**  
    * Update the specified resource in storage.  
    *  
    * @param \Illuminate\Http\Request $request  
    * @param int $id  
    * @return \Illuminate\Http\Response  
    */  
   public function update(barang $barang)  
   {  
     $barang->update([  
       'nama_barang' => request('nama_barang'),  
       'stock' => request('stock')  
     ]);  
     return redirect('/barang');  
   }  
   /**  
    * Remove the specified resource from storage.  
    *  
    * @param int $id  
    * @return \Illuminate\Http\Response  
    */  
   public function destroy(barang $barang)  
   {  
       $barang->delete();  
     return redirect('/barang');  
   }  
 }  

6. buka file web.php pada folder project/routes/web.php
ketikan

 Route::get('/barang','BarangController@index');  
 Route::post('/masuk','BarangController@store');  
 Route::delete('/barang/{barang}/delete','BarangController@destroy')->name('barang.destroy');  
 Route::patch('/barang/{barang}/edit','BarangController@update')->name('barang.update');
 Route::get('/barang/form/{id}','BarangController@edit')->name('barang.edit');  

7. buatlah file dengan nama"barang.blade.php" dan di letakan di project/resources/view
lalu ketikan seperti dibawah ini

 <!DOCTYPE html>  
 <html lang="en">  
 <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>Daftar Barang KuloLaku</title>  
 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto|Varela+Round">  
 <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">  
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">  
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">  
 <link rel="stylesheet" type="text/css" href="{{asset('css/style.css') }}">  
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>  
 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>  
 </head>  
 <body>  
   <div class="container">  
     <div class="table-wrapper">  
       <div class="table-title">  
         <div class="row">  
           <div class="col-sm-6">  
       <h2>Daftar <b>Barang</b></h2>  
      </div>  
      <div class="col-sm-6">  
       <a href="#addEmployeeModal" class="btn btn-success" data-toggle="modal"><i class="material-icons">&#xE147;</i> <span>CREATE</span></a>  
      </div>  
         </div>  
       </div>  
       <table class="table table-striped table-hover">  
         <thead>  
           <tr>  
             <th>Nama</th>  
             <th>Stock</th>  
             <th>Created At</th>  
             <th>Update At</th>  
             <th>Actions</th>  
           </tr>  
         </thead>  
         <tbody>  
          @foreach($data as $barang)  
           <tr>  
             <td>{{$barang->nama_barang}}</td>  
             <td>{{$barang->stock}}</td>  
             <td>{{$barang->created_at}}</td>  
             <td>{{$barang->updated_at}}</td>  
             <td><form action="{{ route ('barang.edit',$barang)}}" method="get">  
               {{ csrf_field() }}  
              <button type="submit" class="btn btn-primary btn-sm" style="float:left;">Update</button>  
             </form>  
               <form action="{{ route('barang.destroy',$barang) }}" method="post">  
               {{ csrf_field() }}  
                <input type="hidden" name="_method" value="DELETE">  
                <button type="submit" class="btn btn-danger btn-sm" style="margin-left:3px;">Delete</button>  
               </form>  
             </td>  
           </tr>  
          @endforeach  
         </tbody>  
       </table>  
       <div class="footer">  
        <table>  
         <tr>  
          <td> <form action="/">  
           <button type="submit" class="btn btn-primary btn-sm">Belanja</button>  
           </form></td>  
          <td><form action="/barang">  
           <button type="submit" class="btn btn-primary btn-sm">Daftar Barang</button>  
           </form></td>  
         </tr>  
        </table>  
       </div>  
     </div>  
   </div>  
  <!-- Membuat daftar barang -->  
  <div id="addEmployeeModal" class="modal fade">  
   <div class="modal-dialog">  
    <div class="modal-content">  
     <form method="POST" action="/masuk">  
      {{ csrf_field() }}  
      <div class="modal-header">        
       <h4 class="modal-title">CREATE</h4>  
       <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>  
      </div>  
      <div class="modal-body">       
       <div class="form-group">   
        <label>Name</label>  
        <input type="text" class="form-control" name="nama_barang">  
       </div>  
       <div class="form-group">  
        <label>Stock</label>  
        <input type="number" class="form-control" name="stock">  
       </div>     
      </div>  
      <div class="modal-footer">  
       <input type="submit" class="btn btn-success" value="Add">  
       <input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">  
      </div>  
     </form>  
    </div>  
   </div>  
  </div>  
 </body>  
 </html>                                  

10. buatlah file formbarang.blade.php pada project/resource/view/formbarang.blade.php
lalu ketikan seperti dibawah ini

 <!DOCTYPE html>  
 <html lang="en">  
 <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>Daftar Barang KuloLaku</title>  
 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto|Varela+Round">  
 <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">  
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">  
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">  
 <link rel="stylesheet" type="text/css" href="{{asset('css/style.css') }}">  
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>  
 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>  
 </head>  
 <body>  
   <div class="container">  
     <div class="table-wrapper">  
       <div class="table-title">  
         <div class="row">  
           <div class="col-sm-6">  
            <h2>Form <b>Barang</b></h2>  
           </div>  
         </div>  
       </div>  
      <form method="POST" action="{{ route('barang.update',$barang) }}">    {{ csrf_field() }}  
           {{ method_field('PATCH') }}  
      <div class="modal-body">  
       <div class="form-group">  
        <label>Name</label>  
        <input type="text" class="form-control" name="nama_barang" value="{{$barang->nama_barang}}">  
       </div>  
       <div class="form-group">  
        <label>Stock</label>  
        <input type="number" class="form-control" name="stock" value="{{$barang->stock}}">  
       </div>       
      </div>  
      <div class="modal-footer">  
       <input type="submit" class="btn btn-default" data-dismiss="modal" value="Cancel" action="/barang">  
       <input type="submit" class="btn btn-info" value="Save">  
     </div>  
    </form>  
   </div>  
  </div>  

9. disini saya juga mengguanakan css untuk memperbagus tampilanya .
buat file style.css pada folder project/public/css  lalu ketikan

  body {  
   color: #566787;  
   background: #f5f5f5;  
   font-family: 'Varela Round', sans-serif;  
   font-size: 13px;  
  }  
  .table-wrapper {  
   background: #fff;  
   padding: 20px 25px;  
   margin: 30px 0;  
   border-radius: 3px;  
   box-shadow: 0 1px 1px rgba(0,0,0,.05);  
   }  
  .table-title {      
   padding-bottom: 15px;  
   background: #435d7d;  
   color: #fff;  
   padding: 16px 30px;  
   margin: -20px -25px 10px;  
   border-radius: 3px 3px 0 0;  
   }  
   .table-title h2 {  
   margin: 5px 0 0;  
   font-size: 24px;  
  }  
  .table-title .btn-group {  
   float: right;  
  }  
  .table-title .btn {  
   color: #fff;  
   float: right;  
   font-size: 13px;  
   border: none;  
   min-width: 50px;  
   border-radius: 2px;  
   border: none;  
   outline: none !important;  
   margin-left: 10px;  
  }  
  .table-title .btn i {  
   float: left;  
   font-size: 21px;  
   margin-right: 5px;  
  }  
  .table-title .btn span {  
   float: left;  
   margin-top: 2px;  
  }  
   table.table tr th, table.table tr td {  
     border-color: #e9e9e9;  
   padding: 12px 15px;  
   vertical-align: middle;  
   }  
  table.table tr th:first-child {  
   width: 200px;  
  }  
  table.table tr th:last-child {  
   width: 200px;  
  }  
   table.table-striped tbody tr:nth-of-type(odd) {  
    background-color: #fcfcfc;  
  }  
  table.table-striped.table-hover tbody tr:hover {  
   background: #f5f5f5;  
  }  
   table.table th i {  
     font-size: 13px;  
     margin: 0 5px;  
     cursor: pointer;  
   }   
   table.table td:last-child i {  
   opacity: 0.9;  
   font-size: 22px;  
     margin: 0 5px;  
   }  
  table.table td a {  
   font-weight: bold;  
   color: #566787;  
   display: inline-block;  
   text-decoration: none;  
   outline: none !important;  
  }  
  table.table td a:hover {  
   color: #2196F3;  
  }  
  table.table td a.edit {  
     color: #FFC107;  
   }  
   table.table td a.delete {  
     color: #F44336;  
   }  
   table.table td i {  
     font-size: 19px;  
   }  
  table.table .avatar {  
   border-radius: 50%;  
   vertical-align: middle;  
   margin-right: 10px;  
  }  
   .pagination {  
     float: right;  
     margin: 0 0 5px;  
   }  
   .pagination li a {  
     border: none;  
     font-size: 13px;  
     min-width: 30px;  
     min-height: 30px;  
     color: #999;  
     margin: 0 2px;  
     line-height: 30px;  
     border-radius: 2px !important;  
     text-align: center;  
     padding: 0 6px;  
   }  
   .pagination li a:hover {  
     color: #666;  
   }   
   .pagination li.active a, .pagination li.active a.page-link {  
     background: #03A9F4;  
   }  
   .pagination li.active a:hover {      
     background: #0397d6;  
   }  
  .pagination li.disabled i {  
     color: #ccc;  
   }  
   .pagination li i {  
     font-size: 16px;  
     padding-top: 6px  
   }  
   .hint-text {  
     float: left;  
     margin-top: 10px;  
     font-size: 13px;  
   }    
  /* Modal styles */  
  .modal .modal-dialog {  
   max-width: 400px;  
  }  
  .modal .modal-header, .modal .modal-body, .modal .modal-footer {  
   padding: 20px 30px;  
  }  
  .modal .modal-content {  
   border-radius: 3px;  
  }  
  .modal .modal-footer {  
   background: #ecf0f1;  
   border-radius: 0 0 3px 3px;  
  }  
   .modal .modal-title {  
     display: inline-block;  
   }  
  .modal .form-control {  
   border-radius: 2px;  
   box-shadow: none;  
   border-color: #dddddd;  
  }  
  .modal textarea.form-control {  
   resize: vertical;  
  }  
  .modal .btn {  
   border-radius: 2px;  
   min-width: 100px;  
  }   
  .modal form label {  
   font-weight: normal;  
  }  

untuk melihat hasilnya coba anda buka cmd lalu ketikan

 php artisan serve  


lalu coba ketikan localhost:8000/barang pada browser anda

maka akan muncul seperti dibawah ini


uji coba lah crud yang anda buat mulai dari create update lalu delete. untuk tombol belanja dan Daftar barang belum bisa digunakan karena itu nanti hanya untuk mempermudah pindah antar view barang dan view pembeli

okee jika sudah maka kita lanjut ke pembeli.

10. seperti langkah di awal kita buat terlebih dahulu table databasenya dengan mengetikan pada cmd anda

 php artisan make:migration create_pembelis_table  

lalu ketikan seperti dibawah ini

 public function up()  
   {  
     Schema::create('pembelis', function (Blueprint $table) {  
       $table->increments('id');  
       $table->string('nama');  
       $table->string('email');  
       $table->string('nohp');  
       $table->integer('nama_barang');  
       $table->timestamps();  
     });  
   }  

lalu ketikan pada cmd

 php artisan migrate  


11. nah jika sudah berhasil maka kita buat model nya

 php artisan make:model pembeli  

lalu ketikan seperti di bawah ini

 <?php  
 namespace App;  
 use Illuminate\Database\Eloquent\Model;  
 class pembeli extends Model  
 {  
           protected $primarykey = 'id';  
   protected $fillable =['nama','email','nohp','nama_barang'];  
   public function get_namabarang(){  
             return $this->belongsTo('App\\barang', 'nama_barang', 'id');  
   }  
 }  

function get_namabarang berfungsi untuk menghubungkan table barang dengan table pembeli dengan mengambil primary key dari tabel barang dan dijadikan foregin key di table pembeli.

12. edit pada file barang.php seperti dibawah ini

 <?php  
 namespace App;  
 use Illuminate\Database\Eloquent\Model;  
 class barang extends Model  
 {  
   protected $fillable = ['id','nama_barang','stock'];  
   public function get_pembeli(){  
             return $this->hasMany('App\\pembeli', 'nama_barang', 'id');  
   }  
 }  

13. setelah itu buat controller

 php artisan make:controller PembeliController --resource  

lalu ketikan seperti dibawah ini

 <?php  
 namespace App\Http\Controllers;  
 use Illuminate\Http\Request;  
 use App\pembeli;  
 use App\barang;  
 class PembeliController extends Controller  
 {  
   /**  
    * Display a listing of the resource.  
    *  
    * @return \Illuminate\Http\Response  
    */  
   public function index()  
   {  
     $data = pembeli::with('get_namabarang')->get();  
     $datas = barang::All();  
     return view('pembeli',compact('data','datas'));  
   }  
   /**  
    * Show the form for creating a new resource.  
    *  
    * @return \Illuminate\Http\Response  
    */  
   public function create()  
   {  
   }  
   /**  
    * Store a newly created resource in storage.  
    *  
    * @param \Illuminate\Http\Request $request  
    * @return \Illuminate\Http\Response  
    */  
   public function store(Request $request)  
   {  
     $data = new pembeli();  
     $data ->nama = $request->nama;  
     $data ->email = $request->email;  
     $data ->nohp = $request->nohp;  
     $data->nama_barang = $request->nama_barang;  
     $data->save();  
     return redirect('/');  
   }  
   /**  
    * 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)  
   {  
     $pembeli = pembeli::find($id);  
     $datas = barang::All();  
     return view('formbelanja',compact('pembeli','datas'));  
   }  
   /**  
    * Update the specified resource in storage.  
    *  
    * @param \Illuminate\Http\Request $request  
    * @param int $id  
    * @return \Illuminate\Http\Response  
    */  
   public function update(pembeli $pembeli)  
   {  
     $pembeli->update([  
       'nama' => request('nama'),  
       'email' => request('email'),  
       'nohp' => request('nohp'),  
       'nama_barang' => request('nama_barang')  
     ]);  
     return redirect('/');  
   }  
   /**  
    * Remove the specified resource from storage.  
    *  
    * @param int $id  
    * @return \Illuminate\Http\Response  
    */  
   public function destroy(pembeli $pembeli)  
   {  
       $pembeli->delete();  
     return redirect('/');  
   }  
 }  

14. buka folder web.php lalu tambahkan seperti dibawah ini

 Route::get('/','PembeliController@index');  
 Route::post('/daftar','PembeliController@store');  
 Route::delete('/{pembeli}/delete','PembeliController@destroy')->name('pembeli.destroy');  
 Route::patch('/{pembeli}/edit','PembeliController@update')->name('pembeli.update');  
 Route::get('//form/{id}','PembeliController@edit')->name('pembeli.edit');  
 Route::get('/barang','BarangController@index');  
 Route::post('/masuk','BarangController@store');  
 Route::delete('/barang/{barang}/delete','BarangController@destroy')->name('barang.destroy');  
 Route::patch('/barang/{barang}/edit','BarangController@update')->name('barang.update');  
 Route::get('/barang/form/{id}','BarangController@edit')->name('barang.edit');  

15. buat lah file pembeli.blade.php pada project/resources/view lalu tuliskan seperti dibawah ini.

 <!DOCTYPE html>  
 <html lang="en">  
 <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>Daftar Pemebeli KuloLaku</title>  
 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto|Varela+Round">  
 <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">  
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">  
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">  
 <link rel="stylesheet" type="text/css" href="{{asset('css/style.css') }}">  
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>  
 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>  
 </head>  
 <body>  
   <div class="container">  
     <div class="table-wrapper">  
       <div class="table-title">  
         <div class="row">  
           <div class="col-sm-6">  
       <h2>Pendaftaran <b>Pembeli</b></h2>  
      </div>  
      <div class="col-sm-6">  
       <a href="#addEmployeeModal" class="btn btn-success" data-toggle="modal"><i class="material-icons">&#xE147;</i> <span>CREATE</span></a>  
      </div>  
         </div>  
       </div>  
       <table class="table table-striped table-hover">  
         <thead>  
           <tr>  
             <th>Name</th>  
             <th>Email</th>  
             <th>Phone</th>  
             <th>Barang</th>  
             <th>Created At</th>  
             <th>Actions</th>  
           </tr>  
         </thead>  
         <tbody>  
           @foreach($data as $pembeli)  
           <tr>  
             <td>{{$pembeli->nama}}</td>  
             <td>{{$pembeli->email}}</td>  
             <td>{{$pembeli->nohp}}</td>  
             <td>{{$pembeli->get_namabarang->nama_barang}}</td>  
             <td>{{$pembeli->created_at}}</td>  
             <td><form action="{{ route('pembeli.edit',$pembeli) }}" method="get">  
              <button type="submit" class="btn btn-primary btn-sm" style="float:left;">Update</button>  
             </form>  
               <form action="{{ route('pembeli.destroy',$pembeli) }}" method="post">  
               {{ csrf_field() }}  
                <input type="hidden" name="_method" value="DELETE">  
                <button type="submit" class="btn btn-danger btn-sm" style="margin-left:3px;">Delete</button>  
               </form>  
             </td>  
           </tr>  
           @endforeach  
         </tbody>  
       </table>  
       <div class="footer">  
        <table>  
         <tr>  
          <td> <form action="/">  
           <button type="submit" class="btn btn-primary btn-sm">Belanja</button>  
           </form></td>  
          <td><form action="/barang">  
           <button type="submit" class="btn btn-primary btn-sm">Daftar Barang</button>  
           </form></td>  
         </tr>  
        </table>  
       </div>  
     </div>  
   </div>  
  <!-- membuat pembeli -->  
  <div id="addEmployeeModal" class="modal fade">  
   <div class="modal-dialog">  
    <div class="modal-content">  
     <form method="POST" action="/daftar">  
      {{ csrf_field() }}  
      <div class="modal-header">        
       <h4 class="modal-title">CREATE</h4>  
       <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>  
      </div>  
      <div class="modal-body">       
       <div class="form-group">   
        <label>Name</label>  
        <input type="text" class="form-control" name="nama">  
       </div>  
       <div class="form-group">  
        <label>Email</label>  
        <input type="email" class="form-control" name="email">  
       </div>  
       <div class="form-group">  
        <label>Phone</label>  
        <input type="text" class="form-control" name="nohp">  
       </div>  
       <div class="form-group">  
        <label>Barang</label>  
        <select class="form-control" name="nama_barang">  
         @foreach($datas as $barang)  
         <option value="{{$barang->id}}">{{$barang->nama_barang}}</option>  
         @endforeach  
        </select>  
       </div>   
       </div>       
      <div class="modal-footer">  
       <input type="submit" class="btn btn-success" value="Add">  
       <input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">  
      </div>  
     </form>  
    </div>  
   </div>  
  </div>  
 </body>  
 </html>                                  

16. buat file formbelanja.blade.php pada project/resources/view untuk halaman mengedit.
lalu isikan seperti dibawah ini.

 <!DOCTYPE html>  
 <html lang="en">  
 <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>Daftar Pemebeli KuloLaku</title>  
 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto|Varela+Round">  
 <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">  
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">  
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">  
 <link rel="stylesheet" type="text/css" href="{{asset('css/style.css') }}">  
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>  
 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>  
 </head>  
 <body>  
   <div class="container">  
     <div class="table-wrapper">  
       <div class="table-title">  
         <div class="row">  
           <div class="col-sm-6">  
       <h2>Pendaftaran <b>Pembeli</b></h2>  
      </div>  
      <div class="col-sm-6">  
       <a href="#addEmployeeModal" class="btn btn-success" data-toggle="modal"><i class="material-icons">&#xE147;</i> <span>CREATE</span></a>  
      </div>  
         </div>  
       </div>  
       <table class="table table-striped table-hover">  
         <thead>  
           <tr>  
             <th>Name</th>  
             <th>Email</th>  
             <th>Phone</th>  
             <th>Barang</th>  
             <th>Created At</th>  
             <th>Actions</th>  
           </tr>  
         </thead>  
         <tbody>  
           @foreach($data as $pembeli)  
           <tr>  
             <td>{{$pembeli->nama}}</td>  
             <td>{{$pembeli->email}}</td>  
             <td>{{$pembeli->nohp}}</td>  
             <td>{{$pembeli->get_namabarang->nama_barang}}</td>  
             <td>{{$pembeli->created_at}}</td>  
             <td><form action="{{ route('pembeli.edit',$pembeli) }}" method="get">  
              <button type="submit" class="btn btn-primary btn-sm" style="float:left;">Update</button>  
             </form>  
               <form action="{{ route('pembeli.destroy',$pembeli) }}" method="post">  
               {{ csrf_field() }}  
                <input type="hidden" name="_method" value="DELETE">  
                <button type="submit" class="btn btn-danger btn-sm" style="margin-left:3px;">Delete</button>  
               </form>  
             </td>  
           </tr>  
           @endforeach  
         </tbody>  
       </table>  
       <div class="footer">  
        <table>  
         <tr>  
          <td> <form action="/">  
           <button type="submit" class="btn btn-primary btn-sm">Belanja</button>  
           </form></td>  
          <td><form action="/barang">  
           <button type="submit" class="btn btn-primary btn-sm">Daftar Barang</button>  
           </form></td>  
         </tr>  
        </table>  
       </div>  
     </div>  
   </div>  
  <!-- membuat pembeli -->  
  <div id="addEmployeeModal" class="modal fade">  
   <div class="modal-dialog">  
    <div class="modal-content">  
     <form method="POST" action="/daftar">  
      {{ csrf_field() }}  
      <div class="modal-header">        
       <h4 class="modal-title">CREATE</h4>  
       <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>  
      </div>  
      <div class="modal-body">       
       <div class="form-group">   
        <label>Name</label>  
        <input type="text" class="form-control" name="nama">  
       </div>  
       <div class="form-group">  
        <label>Email</label>  
        <input type="email" class="form-control" name="email">  
       </div>  
       <div class="form-group">  
        <label>Phone</label>  
        <input type="text" class="form-control" name="nohp">  
       </div>  
       <div class="form-group">  
        <label>Barang</label>  
        <select class="form-control" name="nama_barang">  
         @foreach($datas as $barang)  
         <option value="{{$barang->id}}">{{$barang->nama_barang}}</option>  
         @endforeach  
        </select>  
       </div>   
       </div>       
      <div class="modal-footer">  
       <input type="submit" class="btn btn-success" value="Add">  
       <input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">  
      </div>  
     </form>  
    </div>  
   </div>  
  </div>  
 </body>  
 </html>                                  

17 . coba ketikan pada cmd anda

 php artisan serve  

lalu ketikan localhost:8000 pada browser anda

coba buat isi data lalu coba edit dan delete .

dan selamat anda telah berhasil membuat CRUD dengan 2 table berelasi


~TERIMA KASIH~

Comments

Post a Comment

Popular posts from this blog

TUTORIAL MENGINSTAL LARAVEL

Penerapan MVC ( Model , View , Controller ) Pada Laravel.