PHP code example of pepperfm / api-responder-for-laravel

1. Go to this page and download the library: Download pepperfm/api-responder-for-laravel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pepperfm / api-responder-for-laravel example snippets


public function __construct(public ResponseContract $json)
{
}

public function index(Request $request)
{
    $users = User::query()->whereIn('id', $request->input('ids'))->get();

    return $this->json->response($users);
}

/*
 * Generate response.data.meta.pagination from first argument of paginated() method  
 */
public function index(Request $request)
{
    $users = User::query()->whereIn('id', $request->input('ids'))->paginate();

    return $this->json->paginated($users);
}

public function index(Request $request)
{
    $users = User::query()->whereIn('id', $request->input('ids'))->paginate();
    $dtoCollection = $users->getCollection()->mapInto(UserDto::class);

    return $this->json->paginated($dtoCollection->toArray(), $users);
}

public function index(Request $request)
{
    $users = User::query()->whereIn('id', $request->input('ids'))->paginate();
    $dtoCollection = $users->getCollection()->mapInto(UserDto::class);

    return $this->json->paginated($dtoCollection->toArray(), $users);
}

public function index(Request $request, ResponseContract $json)
{
    return $json->response($users);
}

public function index(Request $request)
{
    return resolve(ResponseContract::class)->response($users);
}

return \ApiBaseResponder::response($users);
return BaseResponse::response($users);

#[ResponseDataKey]
public function attributeWithoutParam(): JsonResponse
{
    return $this->json->response($this->user); // response.data.entity
}

#[ResponseDataKey('random_key')]
public function attributeWithParam(): JsonResponse
{
    return $this->json->response($this->user); // response.data.random_key
}