This is a simple package to add owner foreign id to models with a trait.
You can install the package via composer:
composer require mnurullahsaglam/laravel-owner- 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;composer testThe MIT License (MIT). Please see License File for more information.