Berikut langkah-langkah dalam membuat CRUD di Laravel :
1. Buat project baru dengan nama "coba"
2. Buat database dengan nama "coba"
3. Jalankan perintah php artisan migrate, akan muncul tabel user di database
4. Buat Controller dengan nama UserController, ketikkan perintah php artisan make:controller UserController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class UserController extends Controller
{
public function index()
{
$data = User::get();
return view('user', ['data'=>$data]);
}
public function form()
{
$data = new User;
return view('form', ['data'=>$data, 'url'=>route('simpan')]);
}
public function hapus($id)
{
User::where('id', '=', $id)->delete();
return redirect()->route('user');
}
public function simpan(Request $request)
{
$validated = $request->validate([
'name' => 'required|unique:users',
'email' => 'required|unique:users',
'password' => 'required'
]);
// $input = $request->except(['_token']);
$form['name'] = $request->name;
$form['email'] = $request->email;
$form['password'] = bcrypt($request->password);
User::insert($form);
return redirect()->route('user');
}
public function simpan_edit($id, Request $request)
{
$validated = $request->validate([
'name' => 'required|unique:users,name,'.$id,
'email' => 'required|unique:users,email,'.$id,
'password' => 'required'
]);
$form['name'] = $request->name;
$form['email'] = $request->email;
$form['password'] = bcrypt($request->password);
User::where('id', '=', $request->id)->update($form);
return redirect()->route('user');
}
public function edit($id)
{
$data = User::where('id', '=', $id)->first();
return view('form', ['data'=>$data, 'url'=>url('simpan_edit')."/".$id]);
}
}
5. Buat view di resources/views/ dengan nama user.blade.php.
<!DOCTYPE html>
<html>
<head>
<title>Tabel User</title>
</head>
<body>
<a href="{{route('tambah')}}">Tambah</a>
<br>
<br>
<table border="1" style="padding: 2px;">
<thead>
<tr>
<td>Nama</td>
<td>Email</td>
<td>Password</td>
<td>Aksi</td>
</tr>
</thead>
<tbody>
@foreach($data as $item)
<tr>
<td> {{$item->name}} </td>
<td> {{$item->email}} </td>
<td> {{$item->password}} </td>
<td> <button><a href="{{url('hapus')}}/{{$item->id}}">Hapus</a></button>
<button><a href="{{url('edit')}}/{{$item->id}}">Edit</a></button></td>
</tr>
@endforeach
</tbody>
</table>
</body>
</html>
6. Buat view di resources/views/ dengan nama form.blade.php.
<!DOCTYPE html>
<html>
<head>
<title>Tambah/Edit Data</title>
</head>
<body>
<form action="{{$url}}" method="post">
@csrf
<label>Nama</label>
<br>
<input type="text" name="name" value="{{$data->name}}">
<input type="text" name="id" value="{{$data->id}}" style="display: none;">
<br>
@error('name')
<div>{{ $message }}</div>
@enderror
<br>
<label>Email</label>
<br>
<input type="text" name="email" value="{{$data->email}}">
<br>
@error('email')
<div>{{ $message }}</div>
@enderror
<br>
<label>Password</label>
<br>
<input type="password" name="password">
<br>
@error('password')
<div>{{ $message }}</div>
@enderror
<br>
<button>Simpan</button>
<br>
<br>
</form>
</body>
</html>
7. Tambahkan route di Routes/web.php seperti berikut :
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', 'App\Http\Controllers\UserController@index')->name('user');
Route::get('/tambah', 'App\Http\Controllers\UserController@form')->name('tambah');
Route::get('/hapus/{id}', 'App\Http\Controllers\UserController@hapus')->name('hapus');
Route::get('/edit/{id}', 'App\Http\Controllers\UserController@edit')->name('edit');
Route::post('/simpan', 'App\Http\Controllers\UserController@simpan')->name('simpan');
Route::post('/simpan_edit/{id}', 'App\Http\Controllers\UserController@simpan_edit')->name('simpan_edit');
8. Jalankan aplikasi, kemudian tes semua fitur mulai dari tambah data, hapus, dan edit data.
0 comments:
Post a Comment