BrandController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\Brand;
  5. use App\Models\BrandTranslation;
  6. use App\Models\Product;
  7. use Illuminate\Support\Str;
  8. class BrandController extends Controller
  9. {
  10. /**
  11. * Display a listing of the resource.
  12. *
  13. * @return \Illuminate\Http\Response
  14. */
  15. public function index(Request $request)
  16. {
  17. $sort_search =null;
  18. $brands = Brand::orderBy('name', 'asc');
  19. if ($request->has('search')){
  20. $sort_search = $request->search;
  21. $brands = $brands->where('name', 'like', '%'.$sort_search.'%');
  22. }
  23. $brands = $brands->paginate(15);
  24. return view('backend.product.brands.index', compact('brands', 'sort_search'));
  25. }
  26. /**
  27. * Show the form for creating a new resource.
  28. *
  29. * @return \Illuminate\Http\Response
  30. */
  31. public function create()
  32. {
  33. }
  34. /**
  35. * Store a newly created resource in storage.
  36. *
  37. * @param \Illuminate\Http\Request $request
  38. * @return \Illuminate\Http\Response
  39. */
  40. public function store(Request $request)
  41. {
  42. $brand = new Brand;
  43. $brand->name = $request->name;
  44. $brand->meta_title = $request->meta_title;
  45. $brand->meta_description = $request->meta_description;
  46. if ($request->slug != null) {
  47. $brand->slug = str_replace(' ', '-', $request->slug);
  48. }
  49. else {
  50. $brand->slug = preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', $request->name)).'-'.Str::random(5);
  51. }
  52. $brand->logo = $request->logo;
  53. $brand->save();
  54. $brand_translation = BrandTranslation::firstOrNew(['lang' => env('DEFAULT_LANGUAGE'), 'brand_id' => $brand->id]);
  55. $brand_translation->name = $request->name;
  56. $brand_translation->save();
  57. flash(translate('Brand has been inserted successfully'))->success();
  58. return redirect()->route('brands.index');
  59. }
  60. /**
  61. * Display the specified resource.
  62. *
  63. * @param int $id
  64. * @return \Illuminate\Http\Response
  65. */
  66. public function show($id)
  67. {
  68. //
  69. }
  70. /**
  71. * Show the form for editing the specified resource.
  72. *
  73. * @param int $id
  74. * @return \Illuminate\Http\Response
  75. */
  76. public function edit(Request $request, $id)
  77. {
  78. $lang = $request->lang;
  79. $brand = Brand::findOrFail($id);
  80. return view('backend.product.brands.edit', compact('brand','lang'));
  81. }
  82. /**
  83. * Update the specified resource in storage.
  84. *
  85. * @param \Illuminate\Http\Request $request
  86. * @param int $id
  87. * @return \Illuminate\Http\Response
  88. */
  89. public function update(Request $request, $id)
  90. {
  91. $brand = Brand::findOrFail($id);
  92. if($request->lang == env("DEFAULT_LANGUAGE")){
  93. $brand->name = $request->name;
  94. }
  95. $brand->meta_title = $request->meta_title;
  96. $brand->meta_description = $request->meta_description;
  97. if ($request->slug != null) {
  98. $brand->slug = strtolower($request->slug);
  99. }
  100. else {
  101. $brand->slug = preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', $request->name)).'-'.Str::random(5);
  102. }
  103. $brand->logo = $request->logo;
  104. $brand->save();
  105. $brand_translation = BrandTranslation::firstOrNew(['lang' => $request->lang, 'brand_id' => $brand->id]);
  106. $brand_translation->name = $request->name;
  107. $brand_translation->save();
  108. flash(translate('Brand has been updated successfully'))->success();
  109. return back();
  110. }
  111. /**
  112. * Remove the specified resource from storage.
  113. *
  114. * @param int $id
  115. * @return \Illuminate\Http\Response
  116. */
  117. public function destroy($id)
  118. {
  119. $brand = Brand::findOrFail($id);
  120. Product::where('brand_id', $brand->id)->delete();
  121. foreach ($brand->brand_translations as $key => $brand_translation) {
  122. $brand_translation->delete();
  123. }
  124. Brand::destroy($id);
  125. flash(translate('Brand has been deleted successfully'))->success();
  126. return redirect()->route('brands.index');
  127. }
  128. }