cloudcreativity/laravel-json-api

Single and filter user resource

acrespo123 opened this issue · 4 comments

How use this library to define a route like 'student' and return the authenticated user with the relations of this. I have 'users' resource and route.

In route/api.php

JsonApi::register('v1')->routes(static function(ApiRegistrar $api){
	//Your /users endpoint
	$api->resource('users');
	//Your /student endpoint
	Route::get('/student',	[StudentController::class,'show'])->middleware('auth');
}

In app/Http/Controllers/StudentController.php:

namespace App\Http\Controllers;

use Illuminate\Http\Response;

class StudentController{

	public function show(): Response{
		$student = Auth::user();
		return json_api()->response()->content($student);
	}

}

If you also want the relationships of the current student, then do this:

$student = Auth::user();

$include = ['roles'];
$parameters = new EncodingParameters($include);

return json_api()->response()->withEncodingParameters($parameters)->content($student);

thank you very much, I will try

Closing this as I think the question has been answered.