Category.php_1 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Categories;
  4. use App\Photos;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Support\Facades\Storage;
  8. use Str;
  9. use File;
  10. use Image;
  11. class Category extends Controller
  12. {
  13. //
  14. public function categories(){
  15. if( Auth::user()->role=="restaurant")
  16. $categories = Categories::whereNull('deleted_at')->whereIn('resto_id',[0,Auth::user()->restaurants->id])->orderBy('name','ASC')->get();
  17. else
  18. $categories = Categories::whereNull('deleted_at')->orderBy('name','ASC')->get();
  19. $data = [
  20. 'categories' => $categories
  21. ];
  22. return view('categories.categories',$data);
  23. }
  24. public function new_category(){
  25. return view('categories.category_form');
  26. }
  27. public function save(Request $request)
  28. {
  29. // dd($request->all());
  30. $id = $request->id;
  31. if(empty($id))
  32. $category = new Categories();
  33. else
  34. $category = Categories::find($id);
  35. $category->name = $request->name;
  36. $category->arabic_name = $request->arabic_name;
  37. $category->is_active =1;
  38. $category->resto_id = Auth::user()->role=="restaurant"?Auth::user()->restaurants->id:0;
  39. $category->save();
  40. $id = $category->id;
  41. if($request->hasFile('main_image')){
  42. $logo = $request->file('main_image');
  43. $file_name = "category-".Str::slug($request->name)."-main_image".'-'.time();
  44. $extension = $logo->getClientOriginalExtension();
  45. Storage::disk('main_image')->put($file_name.'.'.$extension, File::get($logo));
  46. $destinationPath = public_path('/uploads/main_image');
  47. $img = Image::make($destinationPath . '/' . $file_name.'.'.$extension)->resize(100, null, function ($constraint) {
  48. $constraint->aspectRatio();
  49. });
  50. $img->save($destinationPath . '/thumbnails/' . $file_name.'.'.$extension);
  51. $main_image = Photos::where('category_id',$id)->where('photo_type','main_image')->first();
  52. if(!$main_image)
  53. $main_image = new Photos();
  54. $main_image->file_name = $file_name.'.'.$extension;
  55. $main_image->category_id = $id;
  56. $main_image->photo_type = 'main_image';
  57. $main_image->resto_id = Auth::user()->restaurants->id;;
  58. $main_image->save();
  59. //$resto->text =
  60. }
  61. if($id > 0)
  62. echo json_encode(array('type' => 'success', 'message'=>"Category is saved successfully."));
  63. else
  64. echo json_encode(array('type' => 'error', 'message'=>"Category is not saved successfully."));
  65. }
  66. public function delete($id){
  67. //$id = CommonMethods::decrypt($id);
  68. $category = Categories::find($id);
  69. $category->deleted_at = date('Y-m-d H:i:s');
  70. $category->save();
  71. }
  72. public function edit($id){
  73. $category = Categories::find($id);
  74. $data = [
  75. 'category' => $category,
  76. ];
  77. return view('categories.category_form',$data);
  78. }
  79. }