
PK 
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Category;
class CategoryController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$categories = Category::getAllCategory();
return view('backend.category.index', compact('categories'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$parent_cats = Category::where('is_parent', 1)->orderBy('title', 'ASC')->get();
return view('backend.category.create', compact('parent_cats'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validatedData = $request->validate([
'title' => 'required|string',
'summary' => 'nullable|string',
'photo' => 'nullable|string',
'status' => 'required|in:active,inactive',
'is_parent' => 'sometimes|in:1',
'parent_id' => 'nullable|exists:categories,id',
'category_sortby' => 'required|integer', // Add this line
'isvisible' => 'required',
]);
$slug = generateUniqueSlug($request->title, Category::class);
$validatedData['slug'] = $slug;
$validatedData['is_parent'] = $request->input('is_parent', 0);
//$validatedData['category_sortby'] = $request->input('category_sortby', 0); // Add this line
$category = Category::create($validatedData);
$message = $category
? 'Category successfully added'
: 'Error occurred, Please try again!';
return redirect()->route('category.index')->with(
$category ? 'success' : 'error',
$message
);
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
// Implement if needed
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$category = Category::findOrFail($id);
$parent_cats = Category::where('is_parent', 1)->get();
return view('backend.category.edit', compact('category', 'parent_cats'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$category = Category::findOrFail($id);
$validatedData = $request->validate([
'title' => 'required|string',
'summary' => 'nullable|string',
'photo' => 'nullable|string',
'status' => 'required|in:active,inactive',
'is_parent' => 'sometimes|in:1',
'parent_id' => 'nullable|exists:categories,id',
'category_sortby' => 'required|integer',
'isvisible' => 'required',
]);
// Only generate new slug if title changed
if ($category->title != $request->title) {
$validatedData['slug'] = generateUniqueSlug($request->title, Category::class);
}
// Ensure is_parent is properly set
$validatedData['is_parent'] = $request->has('is_parent') ? 1 : 0;
// If it's a parent category, set parent_id to null
if ($validatedData['is_parent'] == 1) {
$validatedData['parent_id'] = null;
}
// Make sure category_sortby is included
//$validatedData['category_sortby'] = $request->category_sortby;
$status = $category->update($validatedData);
$message = $status
? 'Category successfully updated'
: 'Error occurred, Please try again!';
//return redirect()->route('category.edit', $category->id)->with(
return redirect()->route('category.index')->with(
$status ? 'success' : 'error',
$message
);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$category = Category::findOrFail($id);
$child_cat_id = Category::where('parent_id', $id)->pluck('id');
$status = $category->delete();
if ($status && $child_cat_id->count() > 0) {
Category::shiftChild($child_cat_id);
}
$message = $status
? 'Category successfully deleted'
: 'Error while deleting category';
return redirect()->route('category.index')->with(
$status ? 'success' : 'error',
$message
);
}
/**
* Get child categories by parent ID.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function getChildByParent(Request $request)
{
$category = Category::findOrFail($request->id);
$child_cat = Category::getChildByParentID($request->id);
if ($child_cat->count() <= 0) {
return response()->json(['status' => false, 'msg' => '', 'data' => null]);
}
return response()->json(['status' => true, 'msg' => '', 'data' => $child_cat]);
}
}


PK 99