<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
kingmaker / laravel-many-to-many-self-relationship example snippets
use Illuminate\Database\Eloquent\Model;
use Kingmaker\Illuminate\Eloquent\Relations\HasBelongsToManySelfRelation;
class Post extends Model {
use HasBelongsToManySelfRelation;
public function relatedPosts()
{
return $this->belongsToManySelf('related_posts', 'post1', 'post2');
}
}
$post = Post::first();
$post->relatedPosts; // returns Collection of Related Posts
Post::find(1)->relatedPosts; // returns Posts with id 2, 3, 4
Post::find(2)->relatedPosts; // returns Posts with id 1, 3, 4
Post::find(3)->relatedPosts; // returns Posts with id 1, 2, 4
Post::find(4)->relatedPosts; // returns Posts with id 1, 2, 3
/**
* create the BelongsToManySelf relation on the same Model via a pivot table
*
* @param string $table Pivot table name
* @param string $pivotKey1 Pivot table foreign key 1
* @param string $pivotKey2 Pivot table foreign key 2
* @param string|null $relatedKey Related key on the parent table
* @param string|null $relation Relation name
* @return BelongsToManySelf
*/
public function belongsToManySelf(string $table, string $pivotKey1, string $pivotKey2, $relatedKey = null, $relation = null)
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.