musamba/red-thread

Red Thread is a laravel package that helps you easily list all relationships within a given model.

1.0.3 2024-03-06 08:56 UTC

This package is auto-updated.

Last update: 2024-12-27 19:23:01 UTC


README

Latest Version on Packagist Software License Total Downloads

RedThread is a simple package that allows you to list your Laravel models relationships.

The two people connected by the red thread are destined lovers, regardless of place, time, or circumstances. This magical cord may stretch or tangle, but never break.

Table of Contents

Installation

You can install the package via composer:

composer require musamba/red-thread

You can publish the config file with:

php artisan vendor:publish --tag="red-thread"

Usage

You simply need to use the HasRedThreads trait in your model and call the relationships method. Here is an example:

use Musamba\RedThread\Traits\HasRedThreads;
use Musamba\RedThread\Attributes\RedThread;

class Book extends Model
{
    use HasRedThreads;
    
    // ...
    
    #[RedThread]
    public function reviews(): HasMany
    {
        return $this->hasMany(Review::class);
    }
    
    #[RedThread]
    public function author(): BelongsTo
    {
        return $this->belongsTo(Author::class);
    }
    
    // ...
}

Calling the provided relationships() static method on a model instance:

Book::relationships();

An array containing all the relationships will be returned.

[
    'reviews' => 'Illuminate\Database\Eloquent\Relations\HasMany',
    'author' => 'Illuminate\Database\Eloquent\Relations\BelongsTo',
]

If the check_for_attribute configuration key is set to false, the package will check the return type of the method, so having a situation like this:

use Musamba\RedThread\Traits\HasRedThreads;

class Book extends Model
{
    use HasRedThreads;
    
    // ...
    
    public function reviews(): HasMany
    {
        return $this->hasMany(Review::class);
    }
    
    public function author(): BelongsTo
    {
        return $this->belongsTo(Author::class);
    }
    
    // ...
}

The same array as above will be returned.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Feel free to contribute to this package and help me to improve it. You can contribute by opening an issue or a pull request.

Roadmap

  • Add artisan command to list relationships via CLI.
  • Add related model class and foreign key information.

Security Vulnerabilities

If you discover a security vulnerability within RedThread, please open an issue.

Credits

License

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