PHP Classes

Laravel Relational Data Model: Establish relations between model objects

Recommend this page to a friend!
  Info   View files Documentation   View files View files (9)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 52 This week: 1All time: 10,608 This week: 571Up
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: 36 packages by
Country: Egypt Egypt
Age: 31
All time rank: 259926 in Egypt Egypt
Week rank: 103 Up1 in Egypt Egypt Up
Innovation award
Innovation award
Nominee: 20x

Documentation

[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 folder image Files  
File Role Description
Files folder imagesrc (3 files, 2 directories)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imagedatabase (1 directory)
Files folder imageTraits (2 files)
  Plain text file hasRelationships.php Class Class source
  Plain text file RelationalServiceProvider.php Class Class source
  Plain text file Relationship.php Class Class source

  Files folder image Files  /  src  /  database  
File Role Description
Files folder imagemigrations (1 file)

  Files folder image Files  /  src  /  database  /  migrations  
File Role Description
  Plain text file relationships.stub Class Class source

  Files folder image Files  /  src  /  Traits  
File Role Description
  Plain text file hasChildren.php Class Class source
  Plain text file hasParents.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:52
This week:1
All time:10,608
This week:571Up