-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added belongs to level via relation (#13)
* add belongs to level via relation
- Loading branch information
Showing
3 changed files
with
98 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Umbrellio\LTree\Relations; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\Concerns\SupportsDefaultModels; | ||
use Illuminate\Database\Eloquent\Relations\Relation; | ||
|
||
class BelongsToLevel extends Relation | ||
{ | ||
use SupportsDefaultModels; | ||
|
||
protected $level; | ||
|
||
protected $relationName; | ||
|
||
public function __construct(Builder $query, Model $parent, int $level, $relationName) | ||
{ | ||
$this->level = $level; | ||
$this->relationName = $relationName; | ||
|
||
parent::__construct($query, $parent); | ||
} | ||
|
||
public function addConstraints() | ||
{ | ||
$this->query->ancestorByLevel($this->level); | ||
} | ||
|
||
public function addEagerConstraints(array $models) | ||
{ | ||
$this->query->ancestorByLevel($this->level); | ||
} | ||
|
||
public function initRelation(array $models, $relation) | ||
{ | ||
foreach ($models as $model) { | ||
$model->setRelation($relation, $this->getDefaultFor($model)); | ||
} | ||
|
||
return $models; | ||
} | ||
|
||
public function match(array $models, Collection $results, $relation) | ||
{ | ||
return $models; | ||
} | ||
|
||
public function getResults() | ||
{ | ||
return $this->query->first(); | ||
} | ||
|
||
protected function newRelatedInstanceFor(Model $parent) | ||
{ | ||
return $this->related->newInstance(); | ||
} | ||
} |
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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Umbrellio\LTree\Traits; | ||
|
||
use Illuminate\Database\Eloquent\Concerns\HasRelationships; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Umbrellio\LTree\Relations\BelongsToLevel; | ||
|
||
/** | ||
* @mixin HasRelationships | ||
* @mixin LTreeModelTrait | ||
* @mixin Model | ||
*/ | ||
trait HasTreeRelationships | ||
{ | ||
protected function belongsToLevel($related, int $level = 1, ?Model $instance = null, ?string $relation = null) | ||
{ | ||
if ($relation === null) { | ||
$relation = $this->guessBelongsToRelation(); | ||
} | ||
|
||
$instance = $instance ?: $this->newRelatedInstance($related); | ||
|
||
return new BelongsToLevel($instance->newQuery(), $this, $level, $relation); | ||
} | ||
} |
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