HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/ste.komma.pro/app/Components/ComponentArea/HasComponentAreasTrait.php
<?php


namespace App\Components\ComponentArea;

use App\Components\Component\Component;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\MorphMany;

/**
 * Trait HasDynamicGroupSectionsTrait
 *
 * Can be used by your models to get their dynamicGroupSections
 *
 * @package App\Components\Section
 */
trait HasComponentAreasTrait
{
    public function componentAreas():MorphMany
    {
        return $this->morphMany(ComponentArea::class, 'componentable');
    }

    // Grab all components of all component areas
    public function components()
    {
        // First we need to get the component areas
        $componentAreas = $this->componentAreas;

        // Return empty collection if no componentAreas are bind
        if($componentAreas->count() == 0) return collect();
        if($componentAreas->count() > 1){
            dd('HasComponentAreasTrait: Functionality hasnt been tested for more then 1 area. I question if the orderBy sort_order still works like it should because there will be duplicate sort_orders');
        }

        return Component::whereIn('component_area_id', $this->componentAreas->pluck('id')->toArray())
            ->orderBy('sort_order')
            ->get();

    }
}