Server : nginx/1.18.0 System : Linux localhost 6.14.3-x86_64-linode168 #1 SMP PREEMPT_DYNAMIC Mon Apr 21 19:47:55 EDT 2025 x86_64 User : www-data ( 33) PHP Version : 8.0.16 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, Directory : /var/www/ecommerce/app/Http/Controllers/Shop/ |
<?php
namespace App\Http\Controllers\Shop;
use App\Models\Category;
use App\Models\PaymentMethod;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
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', [
'categories' => $categories
]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$parent_cats = Category::where('is_parent', 1)->where('status', 'active')->orderBy('title', 'ASC')->get();
$payment_methods = PaymentMethod::all();
return view('backend.category.create', [
'parent_cats' => $parent_cats,
'payment_methods' => $payment_methods,
]);
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'title' => 'string|required',
'summary' => 'string|nullable',
'photo' => 'string|nullable',
'status' => 'required|in:active,inactive',
'is_parent' => 'sometimes|in:1',
'parent_id' => 'nullable|exists:categories,id',
'payment_method_id' => 'nullable|exists:payment_methods,id',
]);
$data = $request->all();
$slug = Str::slug($request->input('title'));
$count = Category::where('slug', $slug)->count();
if ($count > 0) {
$slug = $slug . '-' . date('ymdis') . '-' . mt_rand(0, 999);
}
$data['slug'] = $slug;
$data['is_parent'] = $request->input('is_parent', 0);
if (Category::create($data)) {
request()->session()->flash('success', 'Category successfully added');
} else {
request()->session()->flash('error', 'Error occurred, Please try again!');
}
return redirect()->route('category.index');
}
/**
* Display the specified resource.
*
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public function show($id)
{
}
/**
* 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)->where('status', 'active')->orderBy('title', 'ASC')->get();
$payment_methods = PaymentMethod::all();
return view('backend.category.edit', [
'category' => $category,
'parent_cats' => $parent_cats,
'payment_methods' => $payment_methods,
]);
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @param Request $request
*
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$category = Category::findOrFail($id);
$this->validate($request, [
'title' => 'string|required',
'summary' => 'string|nullable',
'photo' => 'string|nullable',
'status' => 'required|in:active,inactive',
'is_parent' => 'sometimes|in:1',
'parent_id' => 'nullable|exists:categories,id',
'payment_method_id' => 'sometimes|exists:payment_methods,id',
'sort_order' => 'sometimes|numeric'
]);
$data = $request->all();
$slug = Str::slug($request->input('title'));
$data['is_parent'] = $request->input('is_parent', 0);
$data['slug'] = $slug;
if ($category->fill($data)->save()) {
request()->session()->flash('success', 'Category successfully updated');
} else {
request()->session()->flash('error', 'Error occurred, Please try again!');
}
return redirect()->route('category.index');
}
/**
* 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');
// return $child_cat_id;
$status = $category->delete();
if ($status) {
if (count($child_cat_id) > 0) {
Category::shiftChild($child_cat_id);
}
request()->session()->flash('success', 'Category successfully deleted');
} else {
request()->session()->flash('error', 'Error while deleting category');
}
return redirect()->route('category.index');
}
public function getChildByParent(Request $request)
{
// return $request->all();
$category = Category::findOrFail($request->id);
$child_cat = Category::getChildByParentID($request->id);
// return $child_cat;
if (count($child_cat) <= 0) {
return response()->json(['status' => false, 'msg' => '', 'data' => null]);
} else {
return response()->json(['status' => true, 'msg' => '', 'data' => $child_cat]);
}
}
}