PHP code example of staudenmeir / laravel-adjacency-list
1. Go to this page and download the library: Download staudenmeir/laravel-adjacency-list library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
staudenmeir / laravel-adjacency-list example snippets
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('parent_id')->nullable();
});
class User extends Model
{
use \Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;
}
class User extends Model
{
public function getParentKeyName()
{
return 'parent_id';
}
}
class User extends Model
{
public function getLocalKeyName()
{
return 'id';
}
}
class User extends Model
{
public function posts()
{
return $this->hasMany(Post::class);
}
}
class User extends Model
{
public function recursivePosts()
{
return $this->hasManyOfDescendantsAndSelf(Post::class);
}
}
$recursivePosts = User::find($id)->recursivePosts;
$users = User::withCount('recursivePosts')->get();
class User extends Model
{
public function descendantPosts()
{
return $this->hasManyOfDescendants(Post::class);
}
}
class User extends Model
{
public function roles()
{
return $this->belongsToMany(Role::class);
}
}
class User extends Model
{
public function recursiveRoles()
{
return $this->belongsToManyOfDescendantsAndSelf(Role::class);
}
}
$recursiveRoles = User::find($id)->recursiveRoles;
$users = User::withCount('recursiveRoles')->get();
class User extends Model
{
public function descendantRoles()
{
return $this->belongsToManyOfDescendants(Role::class);
}
}
class User extends Model
{
public function tags()
{
return $this->morphToMany(Tag::class, 'taggable');
}
}
class User extends Model
{
public function recursiveTags()
{
return $this->morphToManyOfDescendantsAndSelf(Tag::class, 'taggable');
}
}
$recursiveTags = User::find($id)->recursiveTags;
$users = User::withCount('recursiveTags')->get();
class User extends Model
{
public function descendantTags()
{
return $this->morphToManyOfDescendants(Tag::class, 'taggable');
}
}
class Category extends Model
{
public function posts()
{
return $this->morphedByMany(Post::class, 'categorizable');
}
}
class Category extends Model
{
public function recursivePosts()
{
return $this->morphedByManyOfDescendantsAndSelf(Post::class, 'categorizable');
}
}
$recursivePosts = Category::find($id)->recursivePosts;
$categories = Category::withCount('recursivePosts')->get();
class Category extends Model
{
public function descendantPosts()
{
return $this->morphedByManyOfDescendants(Post::class, 'categorizable');
}
}
User::find($id)->recursivePosts()->withTrashedDescendants()->get();
User::find($id)->recursivePosts()->withIntermediateScope('active', new ActiveScope())->get();
User::find($id)->recursivePosts()->withIntermediateScope(
'depth',
function ($query) {
$query->whereDepth('<=', 10);
}
)->get();
User::find($id)->recursivePosts()->withoutIntermediateScope('active')->get();
class Post extends Model
{
use \Staudenmeir\LaravelCte\Eloquent\QueriesExpressions;
}
class User extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
use \Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;
public function descendantPosts(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep
{
return $this->hasManyDeepFromRelations(
$this->descendants(),
(new static)->posts()
);
}
public function posts()
{
return $this->hasMany(Post::class);
}
}
$descendantPosts = User::find($id)->descendantPosts;
Schema::create('nodes', function (Blueprint $table) {
$table->id();
});
Schema::create('edges', function (Blueprint $table) {
$table->unsignedBigInteger('source_id');
$table->unsignedBigInteger('target_id');
$table->string('label');
$table->unsignedBigInteger('weight');
});
class Node extends Model
{
use \Staudenmeir\LaravelAdjacencyList\Eloquent\HasGraphRelationships;
public function getPivotTableName(): string
{
return 'edges';
}
}
class Node extends Model
{
public function getParentKeyName(): string
{
return 'source_id';
}
public function getChildKeyName(): string
{
return 'target_id';
}
}
class Node extends Model
{
public function getLocalKeyName(): string
{
return 'id';
}
}
class Node extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
use \Staudenmeir\LaravelAdjacencyList\Eloquent\HasGraphRelationships;
public function descendantPosts(): \Staudenmeir\EloquentHasManyDeep\HasManyDeep
{
return $this->hasManyDeepFromRelations(
$this->descendants(),
(new static)->posts()
);
}
public function posts()
{
return $this->hasMany(Post::class);
}
}
$descendantPosts = Node::find($id)->descendantPosts;
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.