oobook/manage-eloquent

管理 Laravel Eloquent 模型关系、表列和列类型

v1.0.2 2024-09-27 14:28 UTC

This package is auto-updated.

Last update: 2024-09-27 14:41:21 UTC


README

Main Latest Version on Packagist GitHub release (latest SemVer) Total Downloads GitHub Workflow Status

此包将帮助您管理 Laravel Eloquent 模型关系、表列和列类型。

安装

您可以通过 composer 安装此包

composer require oobook/manage-eloquent

用法

为了使用 definedRelationships() 方法,您必须定义关系返回类型,例如 \Illuminate\Database\Eloquent\Relations\BelongsTo,如下所示。

<?php

namespace App\Models;

use Oobook\Database\Eloquent\Concerns\ManageEloquent;

class Product extends Model
{
    use ManageEloquent;

    protected $fillable = [
        'name'
    ];


    public function tags(): \Illuminate\Database\Eloquent\Relations\MorphToMany
    {
        return $this->morphToMany(
            Tag::class,
            'taggable'
        );
    }

    public function category(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(Category::class);
    }

}

示例

<?php

$product = new Product();

// RELATIONSHIPS
// get all defined Relationships
$product->definedRelationships(); // ['tags', 'category']
// get relationships by relationship type 
$product->definedRelations('BelongsTo'), // ['category'];
$product->definedRelations(['BelongsTo', 'MorphToMany']), // ['tags', 'category'];

// get relation types
$product->definedRelationsTypes(), 
// [
//     "category" => "BelongsTo"
//     "tags" => "MorphToMany"
// ]

$product->hasRelation('tags') // true;
$product->getRelationType('tags') // \Illuminate\Database\Eloquent\Relations\MorphToMany;

// COLUMNS
$product->hasColumn('name') // true if exists on db table;
$product->getColumns() // get the list of columns of the table;

// get the list of timestamp columns of the table;
$product->getTimestampColumns() 

测试

composer test

更新日志

有关最近更改的更多信息,请参阅 更新日志

贡献

有关详细信息,请参阅 贡献指南

安全

如果您发现任何与安全相关的问题,请通过电子邮件 oguz.bukcuoglu@gmail.com 联系我们,而不是使用问题跟踪器。

致谢

许可证

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

Laravel 包模板

此包是使用 Laravel 包模板 生成的。