Current File : /home/tradevaly/text.tradevaly.com.bd/app/Http/Controllers/Admin/CwpController.php
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Rfq;
use App\Models\Tag;
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->category_id = $request->sorting;
        $cwp->save();
        Toastr::success('Success!','Categoy Added Successfully !');
        return back();

    }
    public function destroy($id){
        $cwp = Cwp::find($id);
        if(Storage::disk('public')->exists('setting/cwp/'.$cwp ->photo))
        {
           Storage::disk('public')->delete('setting/cwp/'.$cwp ->photo);
        }
        $cwp->delete();
        Toastr::error('Success!','Categoy Deleted Successfully !');
        return back();
    }


}