Download[RaggiTech] Laravel >= 6.0 - Relational.
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 |