Looking to hire Laravel developers? Try LaraJobs

laravel-api-response maintained by agrodep

Description
Response util for Laravel
Author
Last update
2025/10/24 06:34 (dev-main)
License
Links
Downloads
21
Tags

Comments
comments powered by Disqus

Laravel API Responses

Laravel API Response api uchun moslangan bo‘lib, mijoz javobni JSON shaklida qaytaruvchi va unga shakl berib jo‘natuvchi kutubxona.

Laravel API Response is a package that helps to provide and render a consistent HTTP JSON responses to API calls as well as converting and formatting exceptions to JSON responses.

Talablar (Requirements)

  • PHP ^8.1
  • Laravel ^10 | ^11

Talqinlar mutonosibligi (Version Compatibility)

Laravel Laravel API Response
10.x 1.x
11.x 1.2.x
12.x 1.3.x

O‘rnatish (Installation)

Install the package via composer:

composer require agrodep/laravel-api-response

Ishlatish (Usage)

RestResponse trait faylini asosiy Controller fayliga yoki kerakli Controller fayliga qo‘shish (Add RestResponse trait to app module Controller file or any controller which is needed)

use Agrodep\ApiResponse\Responses\RestResponse;

class Controller extends Controller {
    use RestResponse;
}

...

class UserController extends Controller {
    public function show() {
        return $this->success([
            'user' => new User();
        ]);
    }
}

Mavjud funksiyalar (Available functions)

Nomi (name) Izoh (description) Status
success Muvaffaqiyatli 200
created Muvaffaqiyatli 201
fail Xatolik yuz berganda [400]
error Ichki xatolik 500
unAuthorized Manzilga ruxsat yo‘q [401]
result Javobda raqam va satrlar moslangan 200
paginated Sahiflangan ro‘yxat 200
paged Sahiflangan ro‘yxat (qo‘shimcha maydonlari bilan) 200

Sovg‘a (Bonus)

This package also provided RestRequest to return json response Request validations

use Agrodep\ApiResponse\Requests\RestRequest;

// class UserRequest extends FormRequest - x
class UserRequest extends RestRequest {

}

PaginationRequest

use Agrodep\ApiResponse\Requests\PaginationRequest;

// class UserRequest extends FormRequest - xxx
class UserRequest extends PaginationRequest {

}

Kengaytirish (Extend)

use Agrodep\ApiResponse\Requests\BuilderPaginator;

class UserService {
    
    public function all() {
        // Paginate users
        $users = User::query()->paginate();
        $items = $users->items();
        
        // Change content of paginated list
        $data = collect($items)->map(function(User $user) {
            return [
                'id' => $user->id,
                'name' => $user->name,
            ];
        });
        
        return new BuilderPaginator($users, $data);
    }
}


class UserController extends Controller {
    ...
    
    public function show() {
        $users = $this->service->all();
        
        // paged/paginated
        return $this->paged('users', $users, []);
    }
}

Foydalanilgan manbalar (References)

  • Testbench Laravel Testing Helper for Packages Development

Foydali havolalar (Links)