芝麻web文件管理V1.00
编辑当前文件:/home/royashxg/bit-alphas-ltd.com/user/project/app/Http/Controllers/Admin/CategoryController.php
middleware('auth:admin'); } //*** JSON Request public function datatables(Request $request) { $datas = Category::orderBy('id','desc')->get(); //--- Integrating This Collection Into Datatables return Datatables::of($datas) ->addColumn('status', function(Category $data) { $status = $data->status == 1 ? __('Activated') : __('Deactivated'); $status_sign = $data->status == 1 ? 'success' : 'danger'; return '
'.$status .'
'.__("Activate").'
'.__("Deactivate").'
'; }) ->addColumn('form', function(Category $data) use ($request) { return '
'.__('Create').'
'; }) ->addColumn('action', function(Category $data) { return '
'.'Actions' .'
'.__("Edit").'
'.__("Delete").'
'; }) ->rawColumns(['form','status','action']) ->toJson(); //--- Returning Json Data To Client Side } public function index() { return view('admin.category.index'); } public function create() { return view('admin.category.create'); } public function store(Request $request) { $rules = [ 'name'=> 'required', 'demo_url_status'=> 'required', 'photo' => 'mimes:jpeg,jpg,png,svg,webp', 'slug' => 'unique:categories|regex:/^[a-zA-Z0-9\s-]+$/', 'demo_url_status'=> 'required' ]; $customs = [ 'name.required' => __('Name field is required.'), 'photo.mimes' => __('Photo Type is Invalid.'), 'slug.unique' => __('This slug has already been taken.'), 'slug.regex' => __('Slug Must Not Have Any Special Characters.'), 'demo_url_status.required' => __('Demo Url field is required.') ]; $validator = Validator::make($request->all(), $rules, $customs); if ($validator->fails()) { return response()->json(array('errors' => $validator->getMessageBag()->toArray())); } //--- Logic Section $data = new Category(); $input = $request->all(); if ($file = $request->file('photo')) { $name = Str::random(8).time().'.'.$file->getClientOriginalExtension(); $file->move('assets/images',$name); $input['photo'] = $name; } if ($request->is_featured == ""){ $input['is_featured'] = 0; } else { $input['is_featured'] = 1; } $input['slug'] = Str::slug($request->slug, '-'); $input['demo_url_status'] = $request->demo_url_status; $data->fill($input)->save(); //--- Logic Section Ends //--- Redirect Section $msg = __('New Data Added Successfully.').' '.'
'.__('View Lists.').'
'; return response()->json($msg); //--- Redirect Section Ends } //*** GET Request public function edit($id) { $data = Category::findOrFail($id); return view('admin.category.edit',compact('data')); } //*** POST Request public function update(Request $request, $id) { //--- Validation Section $rules = [ 'name'=> 'required', 'demo_url_status'=> 'required', 'photo' => 'mimes:jpeg,jpg,png,svg,webp', 'slug' => 'unique:categories,slug,'.$id, 'demo_url_status'=> 'required' ]; $customs = [ 'name.required' => __('Name field is required.'), 'photo.mimes' => __('Photo Type is Invalid.'), 'slug.unique' => __('This slug has already been taken.'), 'demo_url_status.required' => __('Demo Url field is required.') ]; $validator = Validator::make($request->all(), $rules, $customs); if ($validator->fails()) { return response()->json(array('errors' => $validator->getMessageBag()->toArray())); } //--- Validation Section Ends //--- Logic Section $data = Category::findOrFail($id); $input = $request->all(); if ($file = $request->file('photo')) { $name = Str::random(8).time().'.'.$file->getClientOriginalExtension(); $file->move('assets/images',$name); @unlink('/assets/images/'.$data->photo); $input['photo'] = $name; } if ($request->is_featured == ""){ $input['is_featured'] = 0; } else { $input['is_featured'] = 1; } $input['slug'] = Str::slug($request->slug, '-'); $data->update($input); //--- Logic Section Ends //--- Redirect Section $msg = 'Data Updated Successfully.'; return response()->json($msg); //--- Redirect Section Ends } //*** GET Request Status public function status($id1,$id2) { $data = Category::findOrFail($id1); $data->status = $id2; $data->update(); //--- Redirect Section $msg = 'Data Updated Successfully.'; return response()->json($msg); //--- Redirect Section Ends } //*** GET Request Delete public function destroy($id) { $data = Category::findOrFail($id); if($data->subs->count()>0) { //--- Redirect Section $msg = 'Remove its SubCategory first !'; return response()->json($msg); //--- Redirect Section Ends } if($data->products->count()>0) { //--- Redirect Section $msg = 'Remove the products first !'; return response()->json($msg); //--- Redirect Section Ends } //If Photo Doesn't Exist if($data->photo == null){ $data->delete(); //--- Redirect Section $msg = 'Data Deleted Successfully.'; return response()->json($msg); //--- Redirect Section Ends } else{ //If Photo Exist if (file_exists(public_path().'/assets/images/'.$data->photo)) { unlink(public_path().'/assets/images/'.$data->photo); } $data->delete(); cache()->forget('categories'); //--- Redirect Section $msg = 'Data Deleted Successfully.'; return response()->json($msg); //--- Redirect Section Ends } } }