pandorga/owner

允许Eloquent模型相互拥有。支持多对多关系。

1.2 2020-12-16 18:53 UTC

This package is auto-updated.

Last update: 2024-09-17 02:33:52 UTC


README

这是一个简单的包,允许Eloquent模型相互“拥有”或“被其他模型拥有”。支持多对多关系。

示例包括

  1. 用户拥有博客文章。
  2. 用户和团队共同拥有多个文件。
  3. 记录被多个组织拥有。

安装

要求

  • Composer
  • Laravel框架5.7+/6.0+/7.0+

安装

在控制台终端中运行以下命令

$ composer require pandorga/owner

发布迁移和配置文件

$ php artisan vendor:publish --provider="Pandorga\Owner\OwnerServiceProvider"

运行迁移

$ php artisan migrate

使用方法

为Eloquent模型添加必要的特质

如果模型可以是所有者

use Pandorga\Owner\Traits\Owns;
    
class User extends Model
{
    use Owns;
}

如果模型可以被其他模型拥有

use Pandorga\Owner\Traits\HasOwner;
    
class Resource extends Model
{
    use HasOwner;
}

使用方法

“所有者”模型

创建所有权

$user->own($model);

移除所有权

$user->disown($model);

返回父模型拥有的所有模型的集合

$user->owns();

用户是否拥有此模型?

$user->ownsModel($model);

父模型拥有哪种类型的模型?此方法可以接受子模型,或者命名空间类名。

$user->ownsModelType($model); // Use a model
$user->ownsModelType(‘App\Resource’); // Use class name

“被拥有”模型

返回模型所有者的集合

$model->owners();

模型是否被其他模型拥有?

$model->isOwnedBy($owner);

向模型添加所有者

$model->addOwner($owner);

从模型中移除所有者

$model->removeOwner($owner);

从模型中移除所有所有者

$model->removeAllOwners();

安全

如果您发现任何安全相关的问题,请使用问题跟踪器。

致谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件