raggitech/laravel-relational

Laravel - Simple relationships (Parents/Children) for model.

0.0.1 2020-05-10 19:02 UTC

This package is auto-updated.

Last update: 2020-07-11 05:27:18 UTC


README

Latest Stable Version Total Downloads License

Laravel - Simple relationships (Parents/Children) for model.

Example:
$category = Category::find(1);
$post = Post::find(1);
  
// Create/Update Child
$category->attach($post)

// Retrieve Category's Posts
$children = $category->children;

foreach($children as $record){
    dump($record->child->record);
}

Install

Install the latest version using Composer:

$ composer require raggitech/laravel-relational

then publish the migration & migrate

$ php artisan vendor:publish --tag=laravel-relational
$ php artisan migrate

Usage

// An Example : (Post Model)
// Using hasRelationships in Post Model
...
use RaggiTech\Laravel\Relational\hasRelationships;

class Post extends Model
{
    use hasRelationships;
...

Parents

Attach To Parent

// Object
$post->attachTo($category);

// List of Objects
$post->attachTo($category1, $category2);
// OR
$post->attachTo([$category1, $category2]);

Detach From Parent

// Object
$post->detachFrom($category);

// List of Objects
$post->detachFrom($category1, $category2);
// OR
$post->detachFrom([$category1, $category2]);

Retrieve Object Parents

// Single
$parent = $post->parent;

// Multi
$parents = $post->parents;

Children

Attach Child

// Object
$category->attach($post);

// List of Objects
$category->attach($post1, $post2);
// OR
$category->attach([$post1, $post2]);

Detach Child

// Object
$category->detach($post);

// List of Objects
$category->detach($post1, $post2);
// OR
$category->detach([$post1, $post2]);

Retrieve Object Children

// Single
$child = $category->child;

// Multi
$children = $category->children;

License

MIT license