Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isParentOf, isChildOf, depthRelatedTo #232

Merged

Conversation

PaperTurtle
Copy link
Contributor

This merge request add three new methods mentioned in (#179) to the HasRecursiveRelationships Trait.

I've tried to create a test for the depthRelatedTo, but I cannot get it to work.

However, I have created a Laravel Project to test if the methods work accordingly. I've created an example Model:

class Category extends Model
{
    use HasFactory, HasRecursiveRelationships;

    protected $fillable = ['name', 'parent_id'];
}

and then tried the methods out:

Route::get('/setup-multi-level-categories', function () {
    $rootCategory = Category::create(['name' => 'Root Category']);

    $firstLevelCategory = Category::create([
        'name' => 'First Level Category',
        'parent_id' => $rootCategory->id
    ]);

    $secondLevelCategory = Category::create([
        'name' => 'Second Level Category',
        'parent_id' => $firstLevelCategory->id
    ]);

    $thirdLevelCategory = Category::create([
        'name' => 'Third Level Category',
        'parent_id' => $secondLevelCategory->id
    ]);

    return 'Multi-level categories setup completed.';
});

Route::get('/is-parent-of/{parentId}/{childId}', function ($parentId, $childId) {
    $parent = Category::findOrFail($parentId);
    $child = Category::findOrFail($childId);

    return response()->json([
        'isParentOf' => $parent->isParentOf($child)
    ]);
});

Route::get('/is-child-of/{childId}/{parentId}', function ($childId, $parentId) {
    $child = Category::findOrFail($childId);
    $parent = Category::findOrFail($parentId);

    return response()->json([
        'isChildOf' => $child->isChildOf($parent)
    ]);
});

Route::get('/depth-related-to/{categoryId}/{relatedCategoryId}', function ($categoryId, $relatedCategoryId) {
    $category = Category::findOrFail($categoryId);
    $relatedCategory = Category::findOrFail($relatedCategoryId);

    return response()->json([
        'depthRelatedTo' => $category->depthRelatedTo($relatedCategory)
    ]);
});

Everything seems to work correctly. Feedback is welcome.

Thanks in advance.

@staudenmeir
Copy link
Owner

Hi @PaperTurtle,
Thanks, I'll take a look.

@PaperTurtle
Copy link
Contributor Author

Thanks @staudenmeir,
I'm looking forward to your review!


public function isParentOf(Model $model)
{
if (!$this->relationLoaded('children')) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The if {} here (and in the other methods) is not necessary. $this->children automatically loads the relationship if necessary.

@PaperTurtle
Copy link
Contributor Author

@staudenmeir Thanks for the feedback, I've implemented your suggestions.

@staudenmeir staudenmeir merged commit 6f6aed7 into staudenmeir:master Apr 19, 2024
18 of 19 checks passed
@staudenmeir
Copy link
Owner

Thanks! I've renamed the third method to getDepthRelatedTo() and will release a new version soon.

staudenmeir added a commit that referenced this pull request Apr 22, 2024
@staudenmeir
Copy link
Owner

I've released a new version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants