oobook/manage-eloquent

Manage the laravel eloquent model relationships, table columns and column types

v1.0.3 2024-10-01 14:09 UTC

This package is auto-updated.

Last update: 2024-10-31 14:19:10 UTC


README

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

This package will help out you to manage the laravel eloquent model relationships, table columns and column types.

Installation

You can install the package via composer:

composer require oobook/manage-eloquent

Usage

You must define return types of relationships in order to use definedRelationships() method, \Illuminate\Database\Eloquent\Relations\BelongsTo i.e. as following.

<?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);
    }

}

Examples

<?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() 

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email oguz.bukcuoglu@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.