PHP code example of naoray / eloquent-model-analyzer

1. Go to this page and download the library: Download naoray/eloquent-model-analyzer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

naoray / eloquent-model-analyzer example snippets


// 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',
// ]

// 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,
// ]