Giter Club home page Giter Club logo

eloquent-model-analyzer's Introduction

eloquent-model-analyzer

Software License Total Downloads Build Status

Analyzing an Eloquent Model for its relations and columns can be overwhelming. This little library aims to make it as simple as possible.

You probably wonder why you would ever need to analyze your Models at runtime?! All scenarios I can think of are related to analyzing the codebase to generate some code bits. Here are some scenarios where this might come in handy:

Install

composer require naoray/eloquent-model-analyzer

Usage

Getting all relations of a Model

There are three different strategies for getting all relation methods of an Eloquent Model:

  • checking the return types of the methods
  • extracting the return types from the doc method
  • call the method directly and check the instance of what is returned
// User.php
class User extends Model
{
    public function parent()
    {
        return $this->belongsTo(self::class);
    }

    public function posts()
    {
        return $this->hasMany(Post::class, 'user_id');
    }
}

// get relations
// type of $columns is \Illuminate\Support\Collection
$relations = Analyzer::relations(User::class);

// get the first relation
$relation = $relations->first();

// all relations implement the Arrayable interface
$relation->toArray();
// [
//     'relatedClass' => User::class,
//     'type' => \Illuminate\Database\Eloquent\Relations\BelongsTo::class,
//     'foreignKey' => 'parent_id',
//     'ownerKey' => 'id',
//     'methodName' => 'parent',
// ]

The RelationMethod Class forwards all method calls which aren't present on the class directly to the underlying ReflectionMethod class.

Getting all Columns of a Model

// CreateUserTable.php
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email')->unique();
        $table->json('bio')->nullable();
    });
}

// get columns
// type of $columns is \Illuminate\Support\Collection
$columns = Analyzer::columns(User::class);

// get a single column by column name
$column = $columns->get('name');

// all columns implement the Arrayable interface
$column->toArray();
// [
//     'name' => 'name',
//     'type' => \Doctrine\DBAL\Types\StringType::class,
//     'unsigned' => false,
//     'unique' => false,
//     'isForeignKey' => false,
//     'nullable' => false,
//     'autoincrement' => false,
// ]

The Column class forwards all method calls which aren't present on the class directly to the underlying DBAL\Schema\Column Class.

Testing

Run the tests with:

vendor/bin/phpunit

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security-related issues, please email [email protected] instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.

eloquent-model-analyzer's People

Contributors

dependabot[bot] avatar naoray avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.