Created
April 18, 2018 23:09
-
-
Save colindecarlo/09359cc0f9320572320117f84d17fa57 to your computer and use it in GitHub Desktop.
Arbitrarily deeply nested parent child relationships with eloquent
This file contains hidden or 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
<?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