Skip to content

Commit

Permalink
Update PostController.php to return JsonResource instead of JsonResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
MrPunyapal committed Sep 5, 2024
1 parent 07e2534 commit f52b495
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions app/Http/Controllers/Api/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,52 @@
use App\Http\Requests\StorePostRequest;
use App\Http\Requests\UpdatePostRequest;
use App\Models\Post;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Response;

class PostController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(): JsonResponse
public function index(): JsonResource
{
return response()->json(Post::all());
return JsonResource::collection(Post::all());
}

/**
* Store a newly created resource in storage.
*/
public function store(StorePostRequest $request): JsonResponse
public function store(StorePostRequest $request): JsonResource
{
$post = Post::query()->create($request->validated());

return response()->json($post, 201);
return new JsonResource(Post::query()->create($request->validated()));
}

/**
* Display the specified resource.
*/
public function show(Post $post): JsonResponse
public function show(Post $post): JsonResource
{
return response()->json($post);
return new JsonResource($post);
}

/**
* Update the specified resource in storage.
*/
public function update(UpdatePostRequest $request, Post $post): JsonResponse
public function update(UpdatePostRequest $request, Post $post): JsonResource
{
$post->update($request->validated());

return response()->json($post);
return new JsonResource($post);
}

/**
* Remove the specified resource from storage.
*/
public function destroy(Post $post): JsonResponse
public function destroy(Post $post): Response
{
$post->delete();

return response()->json(null, 204);
return response()->noContent();
}
}

0 comments on commit f52b495

Please sign in to comment.