New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

alibori/laravel-api-resource-generator

Package to generate API resources from models.
2,916 22
Install
composer require alibori/laravel-api-resource-generator
Latest Version:v1.4.2
PHP:>=8.2
Laravel:11
Older releases:6 – 10
License:MIT
Last Updated:Dec 10, 2024
Links: GitHub  ·  Packagist
Maintainer: alibori

Laravel API Resource Generator Package

Latest Version on Packagist Tests Passed Total Downloads

This package will help you to generate API resources for your Laravel project.

Installation

You can install the package via composer:

composer require alibori/laravel-api-resource-generator --dev

Usage

All you need to do is run the following command:

php artisan api-resource:generate <model-name>

If you want to generate multiple resources at once, you can pass multiple model names separated by a comma:

php artisan api-resource:generate <model-name>,<model-name>,<model-name>

This command will generate a new resource for the given model/s name/s with the properties defined in the model.

If you want to set the array keys of the resource as camelCase, you will be prompted to do it.

For example, for the model named User you will get the following resource:

<?php

namespace App\Http\Resources;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use JsonSerializable;

/**
 * Resource generated by alibori/laravel-api-resource-generator
 *
 * @property integer $id
 * @property string $name
 * @property string $email
 * @property string $email_verified_at
 * @property string $password
 * @property string $remember_token
 * @property string $created_at
 * @property string $updated_at
 */
class UserResource extends JsonResource
{
     /**
     * Transform the resource into an array.
     *
     * @param  Request  $request
     * @return array|Arrayable|JsonSerializable
     */
    public function toArray($request): array|JsonSerializable|Arrayable
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email,
            'email_verified_at' => $this->email_verified_at,
            'remember_token' => $this->remember_token,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at
        ];
    }
}

Publish config file

If you want to publish the config file to change the default namespace for the generated resources and the directory where the resources will be generated, you can run the following command:

php artisan vendor:publish --provider="Alibori\LaravelApiResourceGenerator\LaravelApiResourceGeneratorServiceProvider" --tag="config"

Related Packages

owowagency/laravel-resources

A package to develop projects faster.

12,080 11
laradic/generators

Code Generators

1,952 0
minhd/solr-client

A PHP Solr Client

1,797 1
nauvalazhar/facade-please

Laravel 5 Facade Generator

566 2

Version History

Version Released PHP Laravel License
v1.4.2 >=8.2 ^11.15 MIT
v1.4.1 >=8.2 ^11.15 MIT
v1.4.0 >=8.1 ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0 MIT
v1.3.2 >=8.1 ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0 MIT
v1.3.1 >=8.1 ^6.0|^7.0|^8.0|^9.0|^10.0 MIT