Uname: Linux business55.web-hosting.com 4.18.0-553.lve.el8.x86_64 #1 SMP Mon May 27 15:27:34 UTC 2024 x86_64
Software: LiteSpeed
PHP version: 8.1.31 [ PHP INFO ] PHP os: Linux
Server Ip: 162.213.251.212
Your Ip: 3.14.248.158
User: allssztx (535) | Group: allssztx (533)
Safe Mode: OFF
Disable Function:
NONE

name : PostCategoryCantroller.php
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Response;
use Illuminate\Support\Str;
use App\Models\PostCategory;

class PostCategoryCantroller extends Controller
{
    //
    public function __construct()
    {
        $this->middleware('auth');
        $this->middleware('isAdmin');
    }

    public function index()
    {
        // $data['fetch'] = PostCategory::all();
        $data['fetch'] = DB::select("SELECT c.`category_id`, c.`category_parent`, c.`category_title`, c.`category_slug`, c.`category_image`, c.`category_status`, cat.`category_title` as parent FROM `postcategories` as c LEFT join `postcategories` cat on cat.`category_id` = c.`category_parent`");

        $data['categories'] = PostCategory::all();

        return view('admin/postcategory',$data);
    }

    public function insert(Request $req)
    {
        if ($req->catstatus == true) 
        {
            $status = 1;
        }
        else
        {
            $status = 0;
        }

        $validation = Validator::make($req->all(),[
            'catname' => 'required|max:30',
            'catimage' => 'image|mimes:jpg,png,jpeg|max:2048'
        ],[
            'catname.required' => 'Category Name Is Required',
            'catimage.image' => 'Must In Image Formate',
            'catimage.mimes' => 'Image Type In JPG, PNG & JPEG',
            'catimage.max' => 'Image One Maximum Size Is 2048',
        ]);

        if(!$validation->passes())
        {
            return response()->json(['error'=>$validation->errors()]);
        }
        else
        {
            $AddCategory = new PostCategory;
            $AddCategory->category_parent = $req->parentcat;
            $AddCategory->category_title = $req->catname;
            $AddCategory->category_slug = Str::slug($req->catname);

            if($req->hasFile('catimage'))
            {
                $file = $req->file('catimage');
                $extension = $file->getClientOriginalExtension();
                $fileName = time().'.'.$extension;

                $file->storeAs('public/images',$fileName);
                $AddCategory->category_image = $fileName;
            }

            $AddCategory->category_status = $status;
            $AddCategory->save();
            return response()->json(['success'=>'Category Added Successfully']);
        }
    }

    public function update(Request $req)
    {

        $validation = Validator::make($req->all(),[
            'CatTitle' => 'required|max:30',
            'CatImg' => 'image|mimes:jpg,png,jpeg|max:2048'
        ],[
            'CatTitle.required' => 'Category Name Is Required',
            'CatImg.image' => 'Must In Image Formate',
            'CatImg.mimes' => 'Image Type In JPG, PNG & JPEG',
            'CatImg.max' => 'Image One Maximum Size Is 2048',
        ]);

        if(!$validation->passes())
        {
            return response()->json(['error'=>$validation->errors()]);
        }
        else
        {
            $id = $req->CatId;
            $UpdtCategory = PostCategory::find($id);
            $UpdtCategory->category_parent = $req->UpdtParentCat;
            $UpdtCategory->category_title = $req->CatTitle;
            $UpdtCategory->category_slug = Str::slug($req->CatTitle);

            if($req->hasFile('CatImg'))
            {
                $file = $req->file('CatImg');
                $extension = $file->getClientOriginalExtension();
                $fileName = time().'.'.$extension;

                $file->storeAs('public/images',$fileName);
                $UpdtCategory->category_image = $fileName;
            }

            elseif($req->removeCatImg == true)
            {
                $UpdtCategory->category_image = null;
            }

            else
            {
                $UpdtCategory->category_image = $req->PrevCatImg;
            }

            $UpdtCategory->category_status = $req->CategoryStatus;
            $UpdtCategory->save();
            return response()->json(['success'=>'Category Updated Successfully']);
        }
    }

    public function delete(Request $req)
    {
        $id = $req->id;
        $DeleteCategory = PostCategory::find($id);
        $DeleteCategory->delete();
    }
}
© 2025 GrazzMean-Shell