File tree Expand file tree Collapse file tree 3 files changed +31
-0
lines changed
Expand file tree Collapse file tree 3 files changed +31
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace App \Http \Controllers \Api ;
4+
5+ use App \Http \Controllers \Controller ;
6+ use App \Models \Author ;
7+
8+ class AuthorController extends Controller
9+ {
10+ public function index ()
11+ {
12+ return Author::paginate ();
13+ }
14+
15+ public function show (Author $ author )
16+ {
17+ return $ author ->append ('stars_count ' );
18+ }
19+ }
Original file line number Diff line number Diff line change 33namespace App \Models ;
44
55use Illuminate \Database \Eloquent \Builder ;
6+ use Illuminate \Database \Eloquent \Casts \Attribute ;
67use Illuminate \Database \Eloquent \Factories \HasFactory ;
78use Illuminate \Database \Eloquent \Model ;
89use Illuminate \Database \Eloquent \Relations \HasMany ;
@@ -63,6 +64,13 @@ public function getStarsCount(): int
6364 );
6465 }
6566
67+ protected function starsCount (): Attribute
68+ {
69+ return Attribute::make (
70+ get: fn () => $ this ->getStarsCount (),
71+ );
72+ }
73+
6674 public function cacheStarsCount (): void
6775 {
6876 cache ()->forget ($ this ->getStarsCountCacheKey ());
Original file line number Diff line number Diff line change 11<?php
22
3+ use App \Http \Controllers \Api \AuthorController ;
34use App \Http \Controllers \Api \PluginController ;
45use Illuminate \Support \Facades \Route ;
56
67Route::get ('plugins ' , [PluginController::class, 'index ' ]);
78Route::get ('plugins/{plugin:slug} ' , [PluginController::class, 'show ' ]);
9+
10+ Route::get ('authors ' , [AuthorController::class, 'index ' ]);
11+ Route::get ('authors/{author:slug} ' , [AuthorController::class, 'show ' ]);
You can’t perform that action at this time.
0 commit comments