Skip to content

Commit 61cd742

Browse files
author
Gareth Redfern
committed
tidy routes and use UserResource for return data
1 parent 0b6cf41 commit 61cd742

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

routes/api.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<?php
22

3-
use Illuminate\Http\Request;
3+
use Illuminate\Support\Facades\Auth;
44
use Illuminate\Support\Facades\Route;
55
use App\Http\Controllers\TokenController;
66
use App\Http\Controllers\AvatarController;
7+
use App\Http\Resources\UserResource;
78
use App\Models\User;
89

910
/*
@@ -20,16 +21,16 @@
2021
Route::post('/sanctum/token', TokenController::class);
2122

2223
Route::middleware(['auth:sanctum'])->group(function () {
23-
Route::get('/users/{user}', function (Request $request) {
24-
return $request->user();
24+
Route::get('/users/auth', function () {
25+
return new UserResource(Auth::user());
2526
});
2627

27-
Route::get('/users', function () {
28-
return User::all();
28+
Route::get('/users/{id}', function ($id) {
29+
return new UserResource(User::findOrFail($id));
2930
});
3031

31-
Route::get('/users/auth', function () {
32-
return auth()->user();
32+
Route::get('/users', function () {
33+
return UserResource::collection(User::paginate());
3334
});
3435

3536
Route::post('/users/auth/avatar', [AvatarController::class, 'store']);

0 commit comments

Comments
 (0)