Created
July 4, 2021 18:09
-
-
Save ThisIsntMyId/7ad65775ef959e470caaed8877c82a7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace App\Traits; | |
trait SelfReferenceTrait | |
{ | |
protected $parentColumn = 'parent_id'; | |
public function parent() | |
{ | |
return $this->belongsTo(static::class); | |
} | |
public function children() | |
{ | |
return $this->hasMany(static::class, $this->parentColumn); | |
} | |
public function allChildren() | |
{ | |
return $this->children()->with('allChildren'); | |
} | |
public function root() | |
{ | |
return $this->parent | |
? $this->parent->root() | |
: $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment