Current File : /home/tradevaly/www/app/Http/Controllers/Admin/CwpController.php
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Product;
use App\Models\CategoryWiseProduct;
use App\Models\Cwp;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Http\Request;
use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Str;
use DataTables;
use App\Http\Controllers\User\UserController;

class CwpController extends Controller
{
    public function index(){

        $data['categories'] = Category::where('parent_id', 0)->latest()->get();
        $data['cwps'] = Cwp::latest()->get();
        return view('backend.pages.setting.home.cwp.index')->with($data);
    }
    public function store(Request $request){
        $request->validate([
            'category_id' => ['required', 'unique:cwps'],
            'sorting' => ['required'],
            'image' => ['required']
        ]);
        $cwp = new  Cwp();
        if($request->hasFile('image'))
        {

        $image=$request->file('image');
        $currentDate=Carbon::now()->toDateString();
        $imageName=$currentDate.'-'.uniqid().'.'.$image->getClientOriginalExtension();

          if($image->getClientOriginalExtension() == 'gif'){
                        Storage::disk('public')->put('setting/cwp/',$image);
                      $imageName = $image->hashName();
                 }
                 else{

                       $bannerImage = Image::make($image)->resize(450,760)->stream();
                        Storage::disk('public')->put('setting/cwp/'.$imageName,$bannerImage);
                 }

        $cwp->photo=$imageName;
        }

        $cwp->category_id = $request->category_id;
        $cwp->sorting = $request->sorting;
        $cwp->save();
        Toastr::success('Success!','Categoy Added Successfully !');
        return back();

    }
    public function edit($id){
        $data['cwp'] = Cwp::findOrFail($id);
        $data['categories'] = Category::where('parent_id', 0)->latest()->get();
        return view('backend.pages.setting.home.cwp.edit')->with($data);
    }
    public function update (Request $request,$id){
        $request->validate([
            'category_id' => ['required', 'unique:cwps'],
            'sorting' => ['required'],
        ]);
        $cwp = Cwp::findOrFail($id);
        if($request->hasFile('image'))
        {

            if(Storage::disk('public')->exists('setting/cwp/'.$cwp ->photo))
            {
               Storage::disk('public')->delete('setting/cwp/'.$cwp ->photo);
            }

            $image=$request->file('image');
            $currentDate=Carbon::now()->toDateString();
            $imageName=$currentDate.'-'.uniqid().'.'.$image->getClientOriginalExtension();

            if($image->getClientOriginalExtension() == 'gif'){
                            Storage::disk('public')->put('setting/cwp/',$image);
                        $imageName = $image->hashName();
                    }
                    else{

                        $bannerImage = Image::make($image)->resize(450,760)->stream();
                            Storage::disk('public')->put('setting/cwp/'.$imageName,$bannerImage);
                    }
            $cwp->photo=$imageName;
        }
        if($request->category_id != $cwp->category_id){
            CategoryWiseProduct::where('cwp_id',$cwp->id)->delete();
        }

        $cwp->category_id = $request->category_id;
        $cwp->sorting = $request->sorting;
        $cwp->save();
        Toastr::success('Success!','Categoy Updated Successfully !');
        return back();
    }
    public function destroy($id){
        $cwp = Cwp::findOrFail($id);
        if(Storage::disk('public')->exists('setting/cwp/'.$cwp ->photo))
        {
           Storage::disk('public')->delete('setting/cwp/'.$cwp ->photo);
        }
        CategoryWiseProduct::where('cwp_id',$cwp->id)->delete();
        $cwp->delete();
        Toastr::error('Success!','Categoy Deleted Successfully !');
        return back();
    }

    public function product_index($id){

        $data['cwp'] = Cwp::findOrFail($id);
        $data['categorywiseproducts'] = CategoryWiseProduct::where('cwp_id',$id)->latest()->get();
        $data['products'] = Product::where('category_id',$data['cwp']->category_id)->latest()->get();
        return view('backend.pages.setting.home.cwp.product.index')->with($data);
    }
    public function product_store(Request $request){
        $request->validate([
            'cwp_id' => ['required', 'exists:cwps,id'],
            'product_id' => ['required', 'exists:products,id'],
            'sorting' => ['required'],
        ]);
        $categorywiseproduct = new  CategoryWiseProduct();
        $categorywiseproduct->cwp_id = $request->cwp_id;
        $categorywiseproduct->product_id = $request->product_id;
        $categorywiseproduct->sorting = $request->sorting;
        $categorywiseproduct->save();
        Toastr::success('Success!','Product Added Successfully !');
        return back();

    }
    public function product_destory($id){
        $cwp = CategoryWiseProduct::findOrFail($id);
        $cwp->delete();
        Toastr::error('Success!','Category wise Product Deleted Successfully !');
        return back();
    }

}