Laracel api resource Conditional Attributes not working, when processing Auth::check()

196 views Asked by At
'likedPosts' => $this->when(Auth::check(), function () {
                    return $this->likedPosts;
                }),

When the current user is logged in or when Auth::check() returns true, the resource response should appear.

But it's not working, I am not sure why. look at the following pictures

enter image description here enter image description here

1

There are 1 answers

0
James Wong On BEST ANSWER

A simple solution would be doing it in web.php, cause api.php do nothing with auth.

Route::prefix('api')->middleware('auth')->group(function() {
    Route::apiResource('/explore/gallery', App\Http\Controllers\PostController::class);
});