mnurullahsaglam/laravel-owner

This is a simple package to add owner foreign id to models with a trait

1.1 2023-03-20 21:49 UTC

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This is a simple package to add owner foreign id to models with a trait.

Installation

You can install the package via composer:

composer require mnurullahsaglam/laravel-owner

Usage

  • Add HasOwner trait to your model.

By default, the owner model is App\Models\User and the owner key is user_id. If you want to change these values, you can override them in your model.

use Illuminate\Database\Eloquent\Model;
use Mnurullahsaglam\LaravelOwner\HasOwner;
use Mnurullahsaglam\LaravelOwner\OwnerSettings;

class Post extends Model
{
    use HasOwner;

    public function getOwnerSettings(): OwnerSettings
    {
        return OwnerSettings::create()
            ->setOwnerModel(App\Models\Owner::class)
            ->setOwnerKey('owner_id');
    }
}

You can access the owner model with owner relationship.

$post = Post::find(1);

return $post->owner;

Testing

composer test

Credits

License

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