Skip to content

HichemBenali/Laravel-Model-Doc

 
 

Repository files navigation

Laravel Model PHPDoc Generator

Latest Stable Version Total Downloads License GitHub Build Status

Generate PHPDoc comments for Laravel Models including database columns, relationships, accessors and query scopes.

Contents

Installation

composer require romanzipp/laravel-model-doc --dev

Configuration

Copy configuration to config folder:

php artisan vendor:publish --provider="romanzipp\ModelDoc\Providers\ModelDocServiceProvider"

Usage

php artisan model-doc:generate

See the configuration file for more specific use cases.

Prepare your models

  1. Add the corresponding table name
  2. Add relation methods return types
  3. Add accessor methods return types
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class MyModel extends Model
{
    protected $table = 'models'; // 1. Add the corresponding table name
    
    public function teams(): HasMany // 2. Add relation methods return types
    {
        return $this->hasMany(Team::class);
    }
    
    public function getNameAttribute(): string // 3. Add accessor methods return types
    {
        return ucfirst($this->name);
    }
}

Example

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

/**
 * @property string $id
 * @property string $title
 * @property string $pretty_title
 * @property string|null $icon
 * @property int $order
 * @property bool $enabled
 * @property array $children
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 *
 * @property \Illuminate\Database\Eloquent\Collection|\App\Models\Team[] $teams
 * @property int|null $teams_count
 *
 * @method static \Illuminate\Database\Eloquent\Builder whereTeamName(string $name)
 */
class MyUser extends Model
{
    protected $table = 'users';

    protected $casts = [
        'children' => 'array',
    ];

    public function teams(): HasMany
    {
        return $this->hasMany(Team::class);
    }

    public function scopeWhereTeamName(Builder $builder, string $name)
    {
        $builder->where('name', $name);
    }

    public function getPrettyTitleAttribute(): string
    {
        return ucfirst($this->title);
    }
}

Set custom path

You can set a custom base path for the generator using the usePath static method.

use Illuminate\Support\ServiceProvider;
use romanzipp\ModelDoc\Services\DocumentationGenerator;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        DocumentationGenerator::usePath(fn () => base_path('app/Models'));
    }
}

See the configuration file for more specific use cases.

Features

  • Generate @property tags from attributes
  • Look for attributes type casts
  • Do not generate attribute @property tag if accessor exists
  • Generate @method tags from relationships
  • Generate @property tags from relationships
  • Generate @property tags from relationship counts
  • Generate @method tags query scopes
  • Generate @property tags from accessors
  • Only generate @property-readonly if accessor has no real attribute or mutator

Testing

./vendor/bin/phpunit

About

Generate PHPDoc comments for Laravel Models

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%