Laravel Relational Data Model: Establish relations between model objects

Recommend this page to a friend!
  Info   Documentation   View files (9)   Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2020-05-10 (2 months ago) Not yet rated by the usersTotal: 45 All time: 9,861 This week: 241
Version License PHP version Categories
laravel-relational 1.0Custom (specified...5PHP 5, Language, Design Patterns, Traits
Description Author

This package can be used to establish relations between model objects.

It provides a trait that can be used by model classes to add functions and variables to perform several types of operations between different model objects that have some kind of relationship between them. Currently it can:

- Attach one or more model objects to a given parent model object
- Detach one or more model objects from a given parent model object
- Retrieve one or more parent object of another object
- Attach one or more child model objects to a given model object
- Detach one child model object from a given parent model object
- Retrieve one or more children model objects from a given parent model object

Innovation Award
PHP Programming Innovation award nominee
May 2020
Number 8
Many applications use databases to store and retrieve application data objects defined using model classes.

Usually data objects of different model classes need to establish relationships with each other.

This package provides a trait that can be used in model classes, so you can hierarchical relationships between parent and child objects.

Manuel Lemos
Picture of Moamen Eltouny
  Performance   Level  
Name: Moamen Eltouny <contact>
Classes: 11 packages by
Country: Egypt Egypt
Innovation award
Innovation award
Nominee: 5x

Details

[RaggiTech] Laravel >= 6.0 - Relational.

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;
...

<a name="parents" id="parents"></a>

Parents

<a name="p_a" id="p_a"></a>

Attach To Parent

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

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

<a name="p_d" id="p_d"></a>

Detach From Parent

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

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

<a name="p_r" id="p_r"></a>

Retrieve Object Parents

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

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

<a name="children" id="children"></a>

Children

<a name="c_a" id="c_a"></a>

Attach Child

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

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

<a name="c_d" id="c_d"></a>

Detach Child

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

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

<a name="c_r" id="c_r"></a>

Retrieve Object Children

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

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

License

MIT license

  Files  
File Role Description
src (3 files, 2 directories)
composer.json Data Auxiliary data
LICENSE Lic. License text
README.md Doc. Documentation

  Files  /  src  
File Role Description
database (1 directory)
Traits (2 files)
   hasRelationships.php Class Class source
   RelationalServiceProvider.php Class Class source
   Relationship.php Class Class source

  Files  /  src  /  database  
File Role Description
migrations (1 file)

  Files  /  src  /  database  /  migrations  
File Role Description
   relationships.stub Class Class source

  Files  /  src  /  Traits  
File Role Description
   hasChildren.php Class Class source
   hasParents.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:45
This week:0
All time:9,861
This week:241

For more information send a message to info at phpclasses dot org.