Current File : //home/tradevaly/demo.tradevaly.com.bd/app/Models/CompanyCategory.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;


class CompanyCategory extends Model
{
    use HasFactory;
    use SoftDeletes;

    protected $fillable = [
        'created_user_id', 'modified_user_id', 'deleted_user_id', 'name', 'status', 'updated_at'
    ];


   
    public function companyCategoryDetails()
    {
        return $this->hasMany('App\Models\CompanyCategoryDetail');
    }

    protected function getCat($conditions  = [])
    {   
        $data = CompanyCategory::where('status', 1)->pluck('name', 'id')->toArray();

        return $data;
    }



    protected function getCatWiseCompanyList( $selectCompanyCat = [], $companyCatCondition = [], $companyCatOrderBy = 'ASC',  $selectCompanyCategorydetails = [], $companyCategoryConditions = [], $companyCategorydetailsLimit = 0)
    {   
        
        // start company  find and re arrange
            $companyInfo = Company::getCompanyList([]);

        // end  company  find and re arrange



         // start product  category to product catogory details and merge with product 

        $data = CompanyCategory::select($selectCompanyCat)
            ->where($companyCatCondition)
            ->orderBy('id', $companyCatOrderBy)
                ->with (['companyCategoryDetails' => function($mm) use ($selectCompanyCategorydetails, $companyCategoryConditions, $companyCategorydetailsLimit){
                        return $mm->select($selectCompanyCategorydetails)
                            ->where($companyCategoryConditions)
                            ->where('status', 1)
                        ->limit($companyCategorydetailsLimit);
                    }]);
        $data = $data->get()->toArray();

        //dd($data);

       // dd($data[0]['product_category_details']);

        $reArrange = [];
        if( isset($data)  && !empty($data) ){

            foreach ($data as $key => $value) {
                
                $reArrange[$value['id']]['cat_id'] = $value['id'];
                $reArrange[$value['id']]['category_name'] = $value['name'];
                $reArrange[$value['id']]['status'] = $value['status'];
                $reArrange[$value['id']]['category_exists_status'] = 0;
                
                if( isset($value['company_category_details']) && !empty($value['company_category_details']) ){
                    
                    foreach ($value['company_category_details'] as $key2 => $value2) {
                        
                        if( !empty($value2['company_id'])){
                            
                            $reArrange[$value['id']]['category_exists_status'] = 1;
                            $reArrange[$value['id']]['company_info'][$value2['company_id']] = !empty($companyInfo[$value2['company_id']]) ? $companyInfo[$value2['company_id']] : [];
                        }

                    }
                }else{
                    $reArrange[$value['id']]['company_info'] = [];
                }

            }
        }

        // end product  category to product catogory details and merge with product

        return $reArrange;

    }

}