laravel-owner maintained by mnurullahsaglam
Description
This is a simple package to add owner foreign id to models with a trait
Author
Last update
2023/07/03 12:53
(dev-main)
License
Downloads
14
Tags
Laravel Owner
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
HasOwnertrait 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.