Membangun aplikasi web di era modern memerlukan pondasi yang kokoh. Framework modern memberikan fleksibilitas luar biasa bagi developer untuk menghadirkan fitur kompleks secara cepat tanpa mengabaikan aspek keamanan dan keandalan sistem.
Tahapan Implementasi & Walkthrough Kode
Berikut adalah langkah demi langkah teknis yang dapat Anda ikuti langsung pada codebase Anda:
Tahap 1: Penulisan Kode & Konfigurasi
/**
* edit
*
* @param mixed $post
* @return void
*/
public function edit(Post $post)
{
return view('post.edit', compact('post'));
}
/**
* update
*
* @param mixed $request
* @param mixed $post
* @return void
*/
public function update(Request $request, Post $post)
{
$this->validate($request, [
'title' => 'required',
'content' => 'required'
]);
//get data post by ID
$post = Post::findOrFail($post->id);
if($request->file('image') == "") {
$post->update([
'title' => $request->title,
'content' => $request->content
]);
} else {
//hapus old image
Storage::disk('local')->delete('public/posts/'.$post->image);
//upload new image
$image = $request->file('image');
$image->storeAs('public/posts', $image->hashName());
$post->update([
'image' => $image->hashName(),
'title' => $request->title,
'content' => $request->content
]);
}
if($post){
//redirect dengan pesan sukses
return redirect()->route('post.index')->with(['success' => 'Data Berhasil Diupdate!']);
}else{
//redirect dengan pesan error
return redirect()->route('post.index')->with(['error' => 'Data Gagal Diupdate!']);
}
}
Tahap 2: Penulisan Kode & Konfigurasi
return view('post.edit', compact('post'));
Tahap 3: Penulisan Kode & Konfigurasi
{{ $post->title }}
Tahap 4: Penulisan Kode & Konfigurasi
$blog->update([
'title' => $request->title,
'content' => $request->content
]);
Tahap 5: Penulisan Kode & Konfigurasi
//hapus old image
Storage::disk('local')->delete('public/posts/'.$post->image);
Tahap 6: Penulisan Kode & Konfigurasi
//upload new image
$image = $request->file('image');
$image->storeAs('public/posts', $image->hashName());
Tahap 7: Penulisan Kode & Konfigurasi
$post->update([
'image' => $image->hashName(), // <— new name file image
'title' => $request->title,
'content' => $request->content
]);
Tahap 8: Penulisan Kode & Konfigurasi
@extends('layouts.app', ['title' => 'Edit Post'])
@section('content')
<div class="container mx-auto mt-10 mb-10">
<div class="bg-white p-5 rounded shadow-sm">
<form action="{{ route('post.update', $post->id) }}" method="POST" enctype="multipart/form-data">
@csrf
@method('PUT')
<div>
<label>GAMBAR POST</label>
<input type="file" name="image"
class="w-full bg-gray-200 p-2 rounded shadow-sm border border-gray-200 focus:outline-none focus:bg-white mt-2">
@error('image')
<div class="bg-red-400 p-2 shadow-sm rounded mt-2">
{{ $message }}
</div>
@enderror
</div>
<div class="mt-5">
<label>JUDUL POST</label>
<input type="text" name="title" value="{{ old('title', $post->title) }}"
class="w-full bg-gray-200 p-2 rounded shadow-sm border border-gray-200 focus:outline-none focus:bg-white mt-2"
placeholder="judul post">
@error('title')
<div class="bg-red-400 p-2 shadow-sm rounded mt-2">
{{ $message }}
</div>
@enderror
</div>
<div class="mt-5">
<label>KONTEN POST</label>
<textarea name="content">{{ old('content', $post->title) }}</textarea>
@error('content')
<div class="bg-red-400 p-2 shadow-sm rounded mt-2">
{{ $message }}
</div>
@enderror
</div>
<div class="mt-5">
<button type="submit"
class="bg-indigo-500 text-white p-2 rounded shadow-sm focus:outline-none hover:bg-indigo-700">UPDATE POST</button>
</div>
</form>
</div>
</div>
<script src="https://cdn.ckeditor.com/4.16.0/standard/ckeditor.js"></script>
<script>
CKEDITOR.replace( 'content' );
</script>
@endsection
Tahap 9: Penulisan Kode & Konfigurasi
<form action="{{ route('post.update', $post->id) }}" method="POST" enctype="multipart/form-data">
Tahap 10: Penulisan Kode & Konfigurasi
@method('PUT')
Tahap 11: Penulisan Kode & Konfigurasi
<input type="text" name="title" value="{{ old('title', $post->title) }}" class="w-full bg-gray-200 p-2 rounded shadow-sm border border-gray-200 focus:outline-none focus:bg-white mt-2" placeholder="judul post">
Selalu gunakan pendekatan deklaratif, validasi input seawal mungkin di boundary layer, dan lakukan logging terstruktur untuk memudahkan observability ketika sistem dijalankan di lingkungan cloud produksi.
Kesimpulan
Dengan mengikuti langkah-langkah terstruktur di atas, implementasi modul Anda akan jauh lebih rapi, aman dari bug umum, serta siap diskalakan ke lingkungan produksi.
Nantikan artikel tutorial praktis lainnya seputar fullstack development, microservices, dan otomasi AI di Blog Engineering SiHubung. Selamat mencoba!