-
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a496423
commit 053c37d
Showing
4 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Staudenmeir\LaravelAdjacencyList\Eloquent\Traits; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
trait HasRecursiveRelationshipHelpers | ||
{ | ||
/** | ||
* Determine if the model is a child of the given model. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @return bool | ||
*/ | ||
public function isChildOf(Model $model): bool | ||
{ | ||
return $this->parent ? $this->parent->is($model) : false; | ||
} | ||
|
||
/** | ||
* Determine if the model is the parent of the given model. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @return bool | ||
*/ | ||
public function isParentOf(Model $model): bool | ||
{ | ||
return $this->children->contains($model); | ||
} | ||
|
||
/** | ||
* Get the depth of the model related to the given model. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @return int|null | ||
*/ | ||
public function getDepthRelatedTo(Model $model): ?int | ||
{ | ||
$thisModel = $this->bloodline->find($this); | ||
$relatedModel = $this->bloodline->find($model); | ||
|
||
if ($thisModel && $relatedModel) { | ||
$depthName = $this->getDepthName(); | ||
|
||
return $thisModel->$depthName - $relatedModel->$depthName; | ||
} | ||
|
||
return null; | ||
} | ||
} |
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