The self::class is supposed to be the model of the related table
//In Person Model
use AppFather;
use AppMother;
public function father() {
$this->belongsTo(Father::class, 'father_id', 'id');
//$this->belongsTo(self::class, 'father_id', 'id');
}
public function mother() {
$this->belongsTo(Mother::class, 'mother_id', 'id');
//$this->belongsTo(self::class, 'mother_id', 'id');
}
//In Father model
use AppPerson;
public function person(){
$this->hasOne(Person::class);
}
//In Mother model
use AppPerson;
public function person(){
$this->hasOne(Person::class);
}
I hope this help.. you can still check this link
https://laravel.com/docs/7.x/eloquent-relationships
I want to create a family tree
Person Table have id
and name
and birth
and father_id
and mother_id
The Person Model is self-relevant
//In Person Model
public function father()
{
$this->belongsTo(self::class, 'father_id', 'id');
}
public function mother()
{
$this->belongsTo(self::class, 'mother_id', 'id');
}
So It can be used in this way.
$person = Person::find(1);
$person->father->name;
$person->mother->name;
$person->father->mother->name;
I was able to get, but I don’t know how to display it.
I want to display it using foreach. Or, another way to write neatly.
I want the layout to look like this
https://jsfiddle.net/tp1Lumkx/1/
but This layout is very long and contains a lot of overlapping places.
so want to use foreach.
or Using Blade’s include
and recursive and so on.
I want to keep it short and show it.
example)
foreach($person as $p){
<tr colspan="?">{{$p->name}}</tr>
}
@include('father_col')
controller
public function show($person_id)
{
$person = Person::find($person_id);
return view('familytree',compact('person'));
}
familytree
It was too hard to fill it all up and I failed.
so want to use foreach.
or Using Blade’s include
and so on.
I want to keep it short and show it.
<table class="table table-bordered">
<thead>
<tr>
<th class="p-0">Own</th>
<th class="p-0">1 generation</th>
<th class="p-0">2 generation</th>
<th class="p-0">3 generation</th>
<th class="p-0">4 generation</th>
<th class="p-0" colspan="2">5 generation</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="32">{{ $person->name }}</td>
<td rowspan="16">{{ $person->father->name }}</td>
<td rowspan="8">{{ $person->father->father->name }}</td>
<td rowspan="4">{{ $person->father->father->father->name }}</td>
<td rowspan="2">{{ $person->father->father->father->father->name }}</td>
<td>{{ $person->father->father->father->father->father->name }}</td>
<td>{{ $person->father->father->father->father->father->birth}}</td>
</tr>
<tr>
<td>{{ $person->father->father->father->father->mother->name }}</td>
<td>{{ $person->father->father->father->father->mother->birth}}</td>
</tr>
<tr>
<td rowspan="2">{{ $person->father->father->father->mother->name}}</td>
<td>{{ $person->father->father->father->mother->father->name}}</td>
<td>{{ $person->father->father->father->mother->father->birth}}</td>
</tr>
<tr>
<td>{{ $person->father->father->father->mother->mother->name}}</td>
<td>{{ $person->father->father->father->mother->mother->birth}}</td>
</tr>
<tr>
<td rowspan="4">{{ $person->father->father->mother->name }}</td>
<td rowspan="2"></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td rowspan="2"></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td rowspan="8"></td>
<td rowspan="4"></td>
<td rowspan="2"></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td rowspan="2"></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td rowspan="4"></td>
<td rowspan="2"></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td rowspan="2"></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td rowspan="16">{{ $person->mother->name }}</td>
<td rowspan="8">{{ $person->mother->father->name }}</td>
<td rowspan="4">{{ $person->mother->father->father->name }}</td>
<td rowspan="2">{{ $person->mother->father->father->father->name }}</td>
<td>{{ $person->mother->father->father->father->father->name }}</td>
<td>{{ $person->mother->father->father->father->father->birth}}</td>
</tr>
<tr>
<td>{{ $person->mother->father->father->father->mother->name }}</td>
<td>{{ $person->mother->father->father->father->mother->birth}}</td>
</tr>
<tr>
<td rowspan="2">{{ $person->mother->father->father->mother->name}}</td>
<td>{{ $person->mother->father->father->mother->father->name}}</td>
<td>{{ $person->mother->father->father->mother->father->birth}}</td>
</tr>
<tr>
<td>{{ $person->mother->father->father->mother->mother->name}}</td>
<td>{{ $person->mother->father->father->mother->mother->birth}}</td>
</tr>
<tr>
<td rowspan="4">{{ $person->mother->father->mother->name }}</td>
<td rowspan="2"></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td rowspan="2"></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td rowspan="8"></td>
<td rowspan="4"></td>
<td rowspan="2"></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td rowspan="2"></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td rowspan="4"></td>
<td rowspan="2"></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td rowspan="2"></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</tbody>
</table>
My English is not good.
So, if you cant understand, sorry
Thank you
sorry, i want to build that layout simply.That’s what I wanted to say.
thnaks, i done. code is too long. sorry