laravel 10 , hierarchy display using 3 models

17 Views Asked by At

In laravel 10 , I have 3 models, Tool, FacilityToolRelation, EquipmentToolRelation, as the name suggest they are connected with their foreign keys , now in a index page for tool i want to show a column for hierarchy, i have used-

{{ $tool->name }}
<ul id="tree1" class="tree" data-parent-id="{{ 
    @foreach ($tool->childs as $child)
        <li  class="branch">{{ $child->name }}</li>
        @if (count($child->childs))
            @include('modules.ParentToolTree', ['childs' => $child->childs,])
        @endif
    @endforeach
</ul>

and this for ParentToolTree.blade.php

<ul>
    @foreach ($childs as $child)
    <li>
        {{ $child->name }}
        @if (count($child->childs))
        @include('modules.ParentToolTree', ['childs' => $child->childs])
        @endif
    </li>
    @endforeach
</ul>

my Tool Model is

<?php
class Tool extends Model {
    
    use HasFactory;

    protected $fillable = [
        'name',
        'code',
        'description',
        'category_id',
        'status'
    ];

    public function assetAddress(): HasOne
    {
        return $this->hasOne(AssetAddress::class, 'asset_id')->where('asset_type', 'tools');
    }

    public function assetGeneralInfo(): HasOne
    {
        return $this->hasOne(AssetGeneralInfo::class, 'asset_id')->where('asset_type', 'tools');
    }

    public function assetPartSuppliesLog(): HasMany
    {
        return $this->hasMany(AssetPartSuppliesLog::class, 'asset_id')->where('asset_type', 'tools');
    }

    public function meterReadings(): HasMany
    {
        return $this->hasMany(MeterReadings::class, 'asset_id')->where('asset_type', 'tools');
    }

    public function assetFiles(): HasMany
    {
        return $this->hasMany(AssetFiles::class, 'asset_id')->where('asset_type', 'tools');
    }
    public function equipment(): BelongsTo
    {
        return $this->belongsTo(Equipment::class);
    }

    public function facilities(): BelongsTo
    {
        return $this->belongsTo(Facility::class, 'facility_tools_relation', 'tool_id', 'facility_id');
    }
}

I want to display the relationship between the tool with the equipment or facility as parent child relationship aka hierarchy display

0

There are 0 best solutions below