Skip to content

Commit f21a9b1

Browse files
committed
add likes_count in PostController
1 parent 6eb1463 commit f21a9b1

2 files changed

Lines changed: 21 additions & 13 deletions

File tree

app/Http/Controllers/PostController.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@ public static function middleware()
1919

2020
public function index()
2121
{
22-
return Post::with(['user', 'comments.user', 'likes'])->latest()->get();
22+
return Post::with(['user', 'comments.user', 'likes'])
23+
->withCount('likes')
24+
->latest()
25+
->get()
26+
->map(function($post) {
27+
return array_merge($post->toArray(), [
28+
'likes_count' => $post->likes_count
29+
]);
30+
});
2331
}
2432

2533
public function store(Request $request)

routes/web.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<?php
22

3-
// use Illuminate\Support\Facades\Route;
4-
// use Inertia\Inertia;
3+
use Illuminate\Support\Facades\Route;
4+
use Inertia\Inertia;
55

6-
// Route::get('/', function () {
7-
// return Inertia::render('welcome');
8-
// })->name('home');
6+
Route::get('/', function () {
7+
return Inertia::render('welcome');
8+
})->name('home');
99

10-
// Route::middleware(['auth', 'verified'])->group(function () {
11-
// Route::get('dashboard', function () {
12-
// return Inertia::render('dashboard');
13-
// })->name('dashboard');
14-
// });
10+
Route::middleware(['auth', 'verified'])->group(function () {
11+
Route::get('dashboard', function () {
12+
return Inertia::render('dashboard');
13+
})->name('dashboard');
14+
});
1515

16-
// require __DIR__.'/settings.php';
17-
// require __DIR__.'/auth.php';
16+
require __DIR__.'/settings.php';
17+
require __DIR__.'/auth.php';

0 commit comments

Comments
 (0)