?
Current File : /home/sanghavirealty/public_html/appazwer/Http/Controllers/CategoriesController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Categories;
use App\Models\Courses;
use App\Models\Users;
use App\Models\Slugemit;
use App\Models\WidgetSetting;

class CategoriesController extends Controller
{
    function showData(){

        $dataviewer = Categories::paginate();   
        return view('categories.category',['data'=>$dataviewer]);
        
        }


        function catManager(){

          $pagination = WidgetSetting::getSpecific('Pagination') ;

                
          $dataviewer = Categories::Where('currentstatus',0)
          ->with('getParentTitle')
          ->paginate($pagination->text2);   
          return view('categories.categoriesmanager',['data'=>$dataviewer]);
          
          }


          function updatecategory($id = 0){ 
            $categoryCombodata = Categories::Where('currentstatus',0)->get();
            
            $dataviewer = Categories::find($id) ;         
            
            
            return view('categories.updatecategories',['data'=>$dataviewer , 'parentData'=> $categoryCombodata]);
          }




          function categoryupdate(Request $req, $id = 0){ 

            $categoryCombodata = Categories::Where('currentstatus',0)->get();
                     

            if ($id != 0 && is_numeric($id)) { 

              $req->validate([
                'title' => 'bail|required',
                'parent' => 'numeric',
                'metadescription'=> 'max:180'
                  ]
                );

              $GetTheCatdata = Categories::find($id) ; 

              if (count($req->all()) > 0){
    
                $GetTheCatdata->title = $req->title ;
                $GetTheCatdata->parentid = $req->parent ;
                $GetTheCatdata->description = $req->description ;
                $GetTheCatdata->metadescription = $req->metadesc ;
               
                $GetTheCatdata->isDirty('name');
                $GetTheCatdata->isDirty('parentid');
                $GetTheCatdata->isDirty('description');
                $GetTheCatdata->isDirty('metadesc');
                $GetTheCatdata->save();

                if($req->hasfile('uploadImage'))
                {         
                  foreach($req->file('uploadImage') as $filedoc)
                  {

                    $getExt = $filedoc->extension();
                    $random = substr(md5(mt_rand()), 0, 5);
                    $fileName = 'cat'.$getCatID->id.'_'.$random.'.'.$getExt ;        
                    $path = $filedoc->storeAs('public/category', $fileName );

                    $catImage = Categories::where('id',$getCatID->id)->update(['image' => $fileName] );                         
        
                  } 
                }
                return redirect('/admin-category-manager')->with('sucess', 'updated sucessfully');


               
              }


            } else {


              if (count($req->all()) > 0 && is_numeric($id)){
    
                $name = $req->title ;
                $finalSlug = Slugemit::slugGenerator($name);
                $dbslugchecker = Categories::Where('slug', $finalSlug)->get();
                if(count($dbslugchecker) == 0) {
        
                  } else {
                    $t=time();
                    $finalSlug = $finalSlug.'_'.$t ;
                  }



                $req->validate([
                  'title' => 'bail|required',
                  'parentid' => 'numeric',
                  'metadescription'=> 'max:180'
                    ]
                  );
        
                  Categories::insert([
                      
                        'slug'=>$finalSlug,
                        'parentid'=>$req->parent,
                        'title'=>$req->title,
                        'description'=>$req->description,
                        'metadescription'=>$req->metadesc,
                       
                      ]);
        
        
                          $getCatID =  Categories::WHERE('currentstatus','0')
                          ->WHERE('slug',$finalSlug)
                          ->orderBy('id', 'DESC')
                          ->first();
        
        
                          if($req->hasfile('uploadImage'))
                            {         
                              foreach($req->file('uploadImage') as $filedoc)
                              {
            
                                $getExt = $filedoc->extension();
                                $random = substr(md5(mt_rand()), 0, 5);
                                $fileName = 'cat'.$getCatID->id.'_'.$random.'.'.$getExt ;        
                                $path = $filedoc->storeAs('public/category', $fileName );
            
                                $catImage = Categories::where('id',$getCatID->id)->update(['image' => $fileName] );                         
                    
                              } 
                            }
                            return redirect('/admin-category-manager')->with('sucess', 'updated sucessfully');

            }

          }




          }








        function categoryCombo ($type) {
          $categoryCombodata = Categories::Where('parentid', $type)
          ->Where('currentstatus',0)
          ->get();
          return $categoryCombodata ;
      
      }
        
        


function DisplayCategory ($slug){


  // display category specific grid

  $dataviewer = Categories::Where('slug',$slug)
  ->Where('currentstatus',0)
  ->with('subcategories.subcategories')
  ->get();

  if (!$dataviewer->isEmpty()){

  $categorytitle = $dataviewer[0]['title'];
  $categoryid = $dataviewer[0]['id'];

  $listingBasedOnCategory = listing::Where('categories_id',$categoryid)
  ->Where('currentstatus',0)
 ->with('categoriesx')
  // ->toSql()
   ->get();

  }else  {
   return  "404:::Error" ;
  }

  return view('categories.subgrid',[
    'SubcategoryGrid'=>$dataviewer,
    'ProductList'=>$listingBasedOnCategory,
    'CategoryTitle'=>$categorytitle
  ]);


 


  

}
    

        function CategoryData(){

          // $dataviewer =  Categories::Where('parentid',0)
          // ->with('subcategories')
          // ->Where('currentstatus',0)
          // ->orderBy('featured', 'DESC')
          // ->toSQL();

          //Working
          // $dataviewer =  Categories::Where('parentid',0) 
          // ->with('subcategories')
          // ->get();

          //Working
          // $dataviewer =  Categories::WhereNull('parentid')
          // ->Where('currentstatus',0)
          // ->orderBy('featured', 'DESC')
          // ->with('subcategories')
          // ->get();

          $dataviewer =  Categories::Where('parentid')
          ->Where('currentstatus',0)
          ->orderBy('featured', 'DESC')
          ->with('subcategories.subcategories' )
          ->get();



          // $query = Post::with(["country" => function($q) use($request){
          //   $q->where("name","=",$request->country);
          // }])->first()


            // dd($dataviewer);
            
         return view('categories.sitecategory',['data'=>$dataviewer]);
        }

    function categoryListFinalClear ($id){

      //deletes the category


      if(is_numeric($id) == true){

        $clear =  Categories::find($id)->delete() ;
        
        return redirect()->back()->with('msg', 'Categories has been deleted');
       
    }else{


        return abort(403) ;

    }






    }


}


      //    $dataviewer = Categories::Where('parentid',0)
      //   ->Where('currentstatus',0)
      //   ->with('Categories.parentid')
      //   ->orderBy('featured', 'DESC')
      // ->toSql();

 
// dd($dataviewer);



//     public function show(Category $category)
    // {
    //     $category->load('subcategories.subcategories');

    //     $subcategoryIDs = [$category->id];
    //     foreach ($category->subcategories as $subcategory) {
    //         $subcategoryIDs[] = $subcategory->id;
    //         foreach ($subcategory->subcategories as $subsubcategory) {
    //             $subcategoryIDs[] = $subsubcategory->id;
    //         }
    //     }}