Skip to content

Instantly share code, notes, and snippets.

@colindecarlo
Created April 18, 2018 23:09
Show Gist options
  • Save colindecarlo/09359cc0f9320572320117f84d17fa57 to your computer and use it in GitHub Desktop.
Save colindecarlo/09359cc0f9320572320117f84d17fa57 to your computer and use it in GitHub Desktop.
Arbitrarily deeply nested parent child relationships with eloquent
<?php
use Illuminate\Eloquent\Model;
class Foo extends Model
{
protected $attributes = [
'parent_id' => null,
];
public function children()
{
return $this->hasMany(static::class, 'parent_id');
}
public function allChildren()
{
return $this->children()->with('allChildren');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment