Skip to content

Commit

Permalink
Added endpoint for authenticated user posts collection
Browse files Browse the repository at this point in the history
Signed-off-by: WillTheDeveloper <willthedeveloper13@gmail.com>
  • Loading branch information
WillTheDeveloper committed Sep 25, 2022
1 parent b4100d7 commit 1cffeed
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
22 changes: 22 additions & 0 deletions app/Http/Resources/UserPostResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class UserPostResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
*/
public function toArray($request)
{
return [
'name' => $this->name,
'posts' => PostResource::collection($this->post)
];
}
}
4 changes: 3 additions & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
Route::get('/users', function () {
return new \App\Http\Resources\UserCollection(\App\Models\User::all());
})->middleware('auth:sanctum');
Route::get('/user/posts', function () {
return new \App\Http\Resources\UserPostResource(auth()->user());
})->middleware('auth:sanctum');
Route::get('/user/{id}', function ($id) {
return new \App\Http\Resources\UserResource(\App\Models\User::findOrFail($id));
})->middleware(['auth:sanctum', 'admin']);
//Route::get('/user')


Route::get('/post/{slug}', function ($slug) {
Expand Down

0 comments on commit 1cffeed

Please sign in to comment.