定制 meeshalk/laravel-ownable-new 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

meeshalk/laravel-ownable-new

最新稳定版本:v1.0.4

Composer 安装命令:

composer require meeshalk/laravel-ownable-new

包简介

Simple trait to check ownership of models/objects

README 文档

README

tests Latest Stable Version Total Downloads License

This trait allows you to check if a model owns some other model in your Laravel application.

Installation

Simply run:

composer require meeshalk/laravel-ownable-new

Then you have to use the OwnsModels trait in the models you want to check if they own other models.

<?php

use Ownable\OwnsModels;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use OwnsModels;
}

You are now ready to use it.

Usage

<?php

use Ownable\OwnsModels;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use OwnsModels;
}

class Video extends Model
{
}

$user = User::first();
$video = Video::first();

// Check If the user owns the video
if ($user->owns($video)){}

// Check If the user doesn't owns the video
if ($user->doesntOwn($video)){}

// Check If the user owns the video but the foreign key is the_user_id
if ($user->owns($video, 'the_user_id')){}

If for some reason the ownership check needs a more complex logic, you can implement the Ownable interface inside your ownable objects and then you can define your custom logic in the isOwnedBy method. When this method is called, the object that called the owns method is passed as an attribute to the isOwnedBy method.

<?php

use Ownable\OwnsModels;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use OwnsModels;
}

use Ownable\Contracts\Ownable;

class Video extends Model implements Ownable
{
    public function isOwnedBy($owner): bool
    {
        if ($owner instanceof User) {
            return $this->someRelationship->user_id == $owner->id;
        }

        return false;
    }
}

$user = User::first();
$video = Video::first();

// Then you can simply call the owns method on the user and it will work.

// Check If the user owns the video
if ($user->owns($video)){}

// Check If the user owns the video but don't use the ownable logic, instead the regular one with the foreign key.
if ($user->owns($video, null, false)){}

统计信息

  • 总下载量: 49
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-21