Modul Belajar Laravel Part 3
Modul Belajar Laravel Part 3
<?php
namespace App\Http\Controllers;
use App\Models\User;
use App\Models\Post;
use App\Models\Like;
use App\Models\Comment;
$post->likes()->attach(auth()->id());
return redirect()->back()->with('success', 'Postingan disukai. ');
}
public function unlike(Post $post)
{
$post->likes()->detach(auth()->id());
return redirect()->back()->with('success', 'Postingan batal disukai. ');
}
public function comment(Request $request)
{
$request->validate([
'post_id'=> 'required|exists:posts,id',
'content'=> 'required|string',
]);
Comment::create([
'post_id'=> $request->post_id,
'user_id'=> auth()->id(),
'content'=> $request->content,
]);
return redirect()->back()->with('success', 'Komentar ditambahkan. ');
}
public function show(string $id): View
{
//get post by ID
$post = Post::findOrFail($id);
<?php
use App\Http\Controllers\AdminController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\PostController;
use App\Models\User;
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 and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('/dashboard', function () {
return view('dashboard');
});
Route::get('login', [AuthController::class,'index'])->name('login');
Route::get('register', [AuthController::class,'register'])->name('register');
Route::post('proses_login', [AuthController::class,'proses_login'])->name('proses_login');
Route::get('logout', [AuthController::class,'logout'])->name('logout');
Route::post('proses_register',[AuthController::class,'proses_register'])->name('proses_register');
6. Buka index.blade.php pada folder posts kemudian ubah kode programnya menjadi
seperti ini
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Postingan</title>
</head>
<body>
@if($post->comments->isNotEmpty())
<h3>Komentar: <b> {{ isset($post->comments) ? $post->comments->count() : 0 }}
</b></h3>
@else
<p>Tidak ada komentar</p>
@endif
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
9. Buat file baru pada Views/posts dengan nama show.blade.php kemudian buat kode
programnya seperti berikut.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Detail Data Postingan</title>
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body style="background: lightgray">
@foreach($post->comments as $comment)
<p><strong>{{ $comment->user->name }}</strong>:{{ $comment->content }}</p>
@endforeach
@else
<p>Tidak ada komentar</p>
@endif
<hr>
<p class="tmt-3">
{!! $post->content !!}
</p>
</div>
</div>
</div>
</div>
</div>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></scrip
t>
</body>
</html>