Viewing File: /home/ubuntu/shop-website-base/app/Http/Controllers/Admin/CategoryController.php
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Helpers\Helper, App\Helpers\EnvEditorHelper;
use DB, Hash, Exception, File, Setting, Validator;
use App\Models\User, App\Models\Settings,App\Models\Category;
use App\Jobs\SendEmailJob;
class CategoryController extends Controller {
/**
* Create a new controller instance.
*
* @return void
*/
protected $paginate_count;
public function __construct() {
$this->middleware('auth:admin');
$this->paginate_count = 10;
}
/**
* @method categories_create()
*
* @uses To create category details
*
* @created Sakthi
*
* @updated
*
* @param
*
* @return return view page
*
*/
public function categories_create() {
$category = new Category;
return view('admin.categories.create')
->with('page', 'categories')
->with('sub_page', 'categories-create')
->with('category', $category);
}
/**
* @method categories_save()
*
* @uses To save the category details of new/existing category object based on details
*
* @created Sakthi
*
* @updated
*
* @param object request - Category Form Data
*
* @return success message
*
*/
public function categories_save(Request $request) {
try {
DB::begintransaction();
$rules = [
'name' => 'required|max:191',
'picture' => 'mimes:jpg,png,jpeg',
'description' => 'max:199',
];
Helper::custom_validator($request->all(),$rules);
$category = $request->category_id ? Category::find($request->category_id) : new Category;
$message = $request->category_id ? tr('category_updated_success') : tr('category_created_success');
$category->name = $request->name ?: $category->name;
$category->description = $request->description ?: '';
$category->unique_id = routefreestring($category->name);
// Upload picture
if($request->hasFile('picture')) {
if($request->category_id) {
Helper::storage_delete_file($category->picture, FILE_PATH_CATEGORY);
// Delete the old pic
}
$category->picture = Helper::storage_upload_file($request->file('picture'), FILE_PATH_CATEGORY);
}
if($category->save()) {
DB::commit();
return redirect(route('admin.categories.view', ['category_id' => $category->id]))->with('flash_success', $message);
}
throw new Exception(tr('category_save_failed'));
} catch(Exception $e){
DB::rollback();
return redirect()->back()->withInput()->with('flash_error', $e->getMessage());
}
}
/**
* @method categories_view()
*
* @uses displays the specified category details based on category id
*
* @created Sakthi
*
* @updated
*
* @param object $request - category Id
*
* @return View page
*
*/
public function categories_view(Request $request) {
try {
$category = Category::find($request->category_id);
if(!$category) {
throw new Exception(tr('category_not_found'), 101);
}
return view('admin.categories.view')
->with('page', 'categories')
->with('sub_page', 'categories-view')
->with('category', $category);
} catch (Exception $e) {
return redirect()->back()->with('flash_error', $e->getMessage());
}
}
/**
* @method categories_delete()
*
* @uses delete the category details based on category id
*
* @created Sakthi
*
* @updated
*
* @param object $request - Category Id
*
* @return response of success/failure details with view page
*
*/
public function categories_delete(Request $request) {
try {
DB::begintransaction();
$category = Category::find($request->category_id);
if(!$category) {
throw new Exception(tr('category_not_found'), 101);
}
if($category->delete()) {
DB::commit();
return redirect()->route('admin.categories.index')->with('flash_success',tr('category_deleted_success'));
}
throw new Exception(tr('category_delete_failed'));
} catch(Exception $e){
DB::rollback();
return redirect()->back()->with('flash_error', $e->getMessage());
}
}
/**
* @method categories_status
*
* @uses To update category status as DECLINED/APPROVED based on category id
*
* @created Sakthi
*
* @updated
*
* @param object $request - Category Id
*
* @return response success/failure message
*
**/
public function categories_status(Request $request) {
try {
DB::beginTransaction();
$category = Category::find($request->category_id);
if(!$category) {
throw new Exception(tr('category_not_found'), 101);
}
$category->status = $category->status ? DECLINED : APPROVED ;
if($category->save()) {
DB::commit();
$message = $category->status ? tr('category_approve_success') : tr('category_decline_success');
return redirect()->back()->with('flash_success', $message);
}
throw new Exception(tr('category_status_change_failed'));
} catch(Exception $e) {
DB::rollback();
return redirect()->route('admin.categories.index')->with('flash_error', $e->getMessage());
}
}
/**
* @method categories_edit()
*
* @uses To display and update category details based on the category id
*
* @created Sakthi
*
* @updated
*
* @param object $request - Category Id
*
* @return redirect view page
*
*/
public function categories_edit(Request $request) {
try {
$category = Category::find($request->category_id);
if(!$category) {
throw new Exception(tr('category_not_found'), 101);
}
return view('admin.categories.edit')
->with('page' , 'categories')
->with('sub_page', 'categories-view')
->with('category', $category);
} catch(Exception $e) {
return redirect()->route('admin.categories.index')->with('flash_error', $e->getMessage());
}
}
/**
* @method categories_index()
*
* @uses To display all category
*
* @created Sakthi
*
* @updated
*
* @param
*
* @return return view page
*
*/
public function categories_index(Request $request) {
$base_query = Category::orderBy('updated_at','desc');
if($request->search_key) {
$base_query = $base_query
->where('name','LIKE','%'.$request->search_key.'%');
}
$categories = $base_query->paginate($this->paginate_count);
return view('admin.categories.index')
->with('page', 'categories')
->with('sub_page' , 'categories-view')
->with('main_page','categories_crud')
->with('page','categories')
->with('sub_page' , 'categories-index')
->with('categories' , $categories);
}
}
Back to Directory
File Manager