Skip to content
On this page

PHP Eloquent

php

Generate Laravel Eloquent model classes with casts, fillable, and relationships from SQL schema

Example Output

Generated from the pet-store sample schema:

Adoption.php

php
<?php
// Generated by @sqldoc/templates/php-eloquent -- DO NOT EDIT

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * Adoption
 *
 * @property int $id
 * @property int $pet_id
 * @property int $owner_id
 * @property string $adopted_at
 * @property float $adoption_fee
 */
class Adoption extends Model
{
    protected $table = 'adoptions';

    public $timestamps = false;

    protected $fillable = [
        'pet_id',
        'owner_id',
        'adopted_at',
        'adoption_fee',
    ];

    protected $casts = [
        'pet_id' => 'integer',
        'owner_id' => 'integer',
        'adopted_at' => 'datetime',
        'adoption_fee' => 'decimal',
    ];

    public function owner(): BelongsTo
    {
        return $this->belongsTo(Owner::class);
    }

    public function pet(): BelongsTo
    {
        return $this->belongsTo(Pet::class);
    }

}

AdoptionsAuditLog.php

php
<?php
// Generated by @sqldoc/templates/php-eloquent -- DO NOT EDIT

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * AdoptionsAuditLog
 *
 * @property int $id
 * @property string $table_name
 * @property string $operation
 * @property ?array $old_data
 * @property ?array $new_data
 * @property string $changed_at
 */
class AdoptionsAuditLog extends Model
{
    protected $table = 'adoptions_audit_log';

    public $timestamps = false;

    protected $fillable = [
        'table_name',
        'operation',
        'old_data',
        'new_data',
        'changed_at',
    ];

    protected $casts = [
        'old_data' => 'array',
        'new_data' => 'array',
        'changed_at' => 'datetime',
    ];

}

Category.php

php
<?php
// Generated by @sqldoc/templates/php-eloquent -- DO NOT EDIT

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
 * Category
 *
 * @property int $id
 * @property string $name
 * @property ?string $description
 */
class Category extends Model
{
    protected $table = 'categories';

    public $timestamps = false;

    protected $fillable = [
        'name',
        'description',
    ];

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

}

LegacyInventory.php

php
<?php
// Generated by @sqldoc/templates/php-eloquent -- DO NOT EDIT

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * LegacyInventory
 *
 * @property int $id
 * @property ?string $item_name
 * @property ?string $old_sku
 * @property ?int $quantity
 */
class LegacyInventory extends Model
{
    protected $table = 'legacy_inventory';

    public $timestamps = false;

    protected $fillable = [
        'item_name',
        'old_sku',
        'quantity',
    ];

    protected $casts = [
        'quantity' => 'integer',
    ];

}

Location.php

php
<?php
// Generated by @sqldoc/templates/php-eloquent -- DO NOT EDIT

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
 * Location
 *
 * @property int $id
 * @property string $name
 * @property string $address
 * @property string $city
 * @property ?string $zip
 */
class Location extends Model
{
    protected $table = 'locations';

    public $timestamps = false;

    protected $fillable = [
        'name',
        'address',
        'city',
        'zip',
    ];

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

}

MedicalRecord.php

php
<?php
// Generated by @sqldoc/templates/php-eloquent -- DO NOT EDIT

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * MedicalRecord
 *
 * @property int $id
 * @property int $pet_id
 * @property string $visit_date
 * @property string $diagnosis
 * @property ?string $treatment
 * @property ?string $vet_name
 */
class MedicalRecord extends Model
{
    protected $table = 'medical_records';

    public $timestamps = false;

    protected $fillable = [
        'pet_id',
        'visit_date',
        'diagnosis',
        'treatment',
        'vet_name',
    ];

    protected $casts = [
        'pet_id' => 'integer',
        'visit_date' => 'date',
    ];

    public function pet(): BelongsTo
    {
        return $this->belongsTo(Pet::class);
    }

}

Owner.php

php
<?php
// Generated by @sqldoc/templates/php-eloquent -- DO NOT EDIT

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
 * Owner
 *
 * @property int $id
 * @property string $name
 * @property string $email
 * @property ?string $phone
 * @property ?string $created_at
 */
class Owner extends Model
{
    protected $table = 'owners';

    public $timestamps = false;

    protected $fillable = [
        'name',
        'email',
        'phone',
        'created_at',
    ];

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

}

Pet.php

php
<?php
// Generated by @sqldoc/templates/php-eloquent -- DO NOT EDIT

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
 * Pet
 *
 * @property int $id
 * @property ?int $category_id
 * @property string $name
 * @property string $sku
 * @property float $price
 * @property ?string $internal_notes
 * @property string $status
 * @property ?string $created_at
 */
class Pet extends Model
{
    protected $table = 'pets';

    public $timestamps = false;

    protected $fillable = [
        'category_id',
        'name',
        'sku',
        'price',
        'internal_notes',
        'status',
        'created_at',
    ];

    protected $casts = [
        'category_id' => 'integer',
        'price' => 'decimal',
    ];

    public function category(): BelongsTo
    {
        return $this->belongsTo(Category::class);
    }

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

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

}

Review.php

php
<?php
// Generated by @sqldoc/templates/php-eloquent -- DO NOT EDIT

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * Review
 *
 * @property int $id
 * @property int $pet_id
 * @property int $owner_id
 * @property int $rating
 * @property ?string $body
 * @property ?int $location_id
 * @property ?string $created_at
 */
class Review extends Model
{
    protected $table = 'reviews';

    public $timestamps = false;

    protected $fillable = [
        'pet_id',
        'owner_id',
        'rating',
        'body',
        'location_id',
        'created_at',
    ];

    protected $casts = [
        'pet_id' => 'integer',
        'owner_id' => 'integer',
        'rating' => 'integer',
        'location_id' => 'integer',
    ];

    public function location(): BelongsTo
    {
        return $this->belongsTo(Location::class);
    }

}

StaffAuditLog.php

php
<?php
// Generated by @sqldoc/templates/php-eloquent -- DO NOT EDIT

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * StaffAuditLog
 *
 * @property int $id
 * @property string $table_name
 * @property string $operation
 * @property ?array $old_data
 * @property ?array $new_data
 * @property string $changed_at
 */
class StaffAuditLog extends Model
{
    protected $table = 'staff_audit_log';

    public $timestamps = false;

    protected $fillable = [
        'table_name',
        'operation',
        'old_data',
        'new_data',
        'changed_at',
    ];

    protected $casts = [
        'old_data' => 'array',
        'new_data' => 'array',
        'changed_at' => 'datetime',
    ];

}

Usage

Add to your sqldoc.config.ts:

typescript
export default {
  dialect: 'postgres',
  schema: ['./schema.sql'],
  codegen: [
    {
      template: 'php-eloquent',
      output: './generated/Models/',
    },
  ],
}

Then run:

bash
sqldoc codegen