AttributeController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\Attribute;
  5. use App\Models\Color;
  6. use App\Models\AttributeTranslation;
  7. use App\Models\AttributeValue;
  8. use CoreComponentRepository;
  9. use Str;
  10. class AttributeController extends Controller
  11. {
  12. /**
  13. * Display a listing of the resource.
  14. *
  15. * @return \Illuminate\Http\Response
  16. */
  17. public function index()
  18. {
  19. CoreComponentRepository::instantiateShopRepository();
  20. CoreComponentRepository::initializeCache();
  21. $attributes = Attribute::orderBy('created_at', 'desc')->get();
  22. return view('backend.product.attribute.index', compact('attributes'));
  23. }
  24. /**
  25. * Show the form for creating a new resource.
  26. *
  27. * @return \Illuminate\Http\Response
  28. */
  29. public function create()
  30. {
  31. }
  32. /**
  33. * Store a newly created resource in storage.
  34. *
  35. * @param \Illuminate\Http\Request $request
  36. * @return \Illuminate\Http\Response
  37. */
  38. public function store(Request $request)
  39. {
  40. $attribute = new Attribute;
  41. $attribute->name = $request->name;
  42. $attribute->save();
  43. $attribute_translation = AttributeTranslation::firstOrNew(['lang' => env('DEFAULT_LANGUAGE'), 'attribute_id' => $attribute->id]);
  44. $attribute_translation->name = $request->name;
  45. $attribute_translation->save();
  46. flash(translate('Attribute has been inserted successfully'))->success();
  47. return redirect()->route('attributes.index');
  48. }
  49. /**
  50. * Display the specified resource.
  51. *
  52. * @param int $id
  53. * @return \Illuminate\Http\Response
  54. */
  55. public function show($id)
  56. {
  57. $data['attribute'] = Attribute::findOrFail($id);
  58. $data['all_attribute_values'] = AttributeValue::with('attribute')->where('attribute_id', $id)->get();
  59. // echo '<pre>';print_r($data['all_attribute_values']);die;
  60. return view("backend.product.attribute.attribute_value.index", $data);
  61. }
  62. /**
  63. * Show the form for editing the specified resource.
  64. *
  65. * @param int $id
  66. * @return \Illuminate\Http\Response
  67. */
  68. public function edit(Request $request, $id)
  69. {
  70. $lang = $request->lang;
  71. $attribute = Attribute::findOrFail($id);
  72. return view('backend.product.attribute.edit', compact('attribute','lang'));
  73. }
  74. /**
  75. * Update the specified resource in storage.
  76. *
  77. * @param \Illuminate\Http\Request $request
  78. * @param int $id
  79. * @return \Illuminate\Http\Response
  80. */
  81. public function update(Request $request, $id)
  82. {
  83. $attribute = Attribute::findOrFail($id);
  84. if($request->lang == env("DEFAULT_LANGUAGE")){
  85. $attribute->name = $request->name;
  86. }
  87. $attribute->save();
  88. $attribute_translation = AttributeTranslation::firstOrNew(['lang' => $request->lang, 'attribute_id' => $attribute->id]);
  89. $attribute_translation->name = $request->name;
  90. $attribute_translation->save();
  91. flash(translate('Attribute has been updated successfully'))->success();
  92. return back();
  93. }
  94. /**
  95. * Remove the specified resource from storage.
  96. *
  97. * @param int $id
  98. * @return \Illuminate\Http\Response
  99. */
  100. public function destroy($id)
  101. {
  102. $attribute = Attribute::findOrFail($id);
  103. foreach ($attribute->attribute_translations as $key => $attribute_translation) {
  104. $attribute_translation->delete();
  105. }
  106. Attribute::destroy($id);
  107. flash(translate('Attribute has been deleted successfully'))->success();
  108. return redirect()->route('attributes.index');
  109. }
  110. public function store_attribute_value(Request $request)
  111. {
  112. $attribute_value = new AttributeValue;
  113. $attribute_value->attribute_id = $request->attribute_id;
  114. $attribute_value->value = ucfirst($request->value);
  115. $attribute_value->save();
  116. flash(translate('Attribute value has been inserted successfully'))->success();
  117. return redirect()->route('attributes.show', $request->attribute_id);
  118. }
  119. public function edit_attribute_value(Request $request, $id)
  120. {
  121. $attribute_value = AttributeValue::findOrFail($id);
  122. return view("backend.product.attribute.attribute_value.edit", compact('attribute_value'));
  123. }
  124. public function update_attribute_value(Request $request, $id)
  125. {
  126. $attribute_value = AttributeValue::findOrFail($id);
  127. $attribute_value->attribute_id = $request->attribute_id;
  128. $attribute_value->value = ucfirst($request->value);
  129. $attribute_value->save();
  130. flash(translate('Attribute value has been updated successfully'))->success();
  131. return back();
  132. }
  133. public function destroy_attribute_value($id)
  134. {
  135. $attribute_values = AttributeValue::findOrFail($id);
  136. AttributeValue::destroy($id);
  137. flash(translate('Attribute value has been deleted successfully'))->success();
  138. return redirect()->route('attributes.show', $attribute_values->attribute_id);
  139. }
  140. public function colors(Request $request) {
  141. $sort_search = null;
  142. $colors = Color::orderBy('created_at', 'desc');
  143. if ($request->search != null){
  144. $colors = $colors->where('name', 'like', '%'.$request->search.'%');
  145. $sort_search = $request->search;
  146. }
  147. $colors = $colors->paginate(10);
  148. return view('backend.product.color.index', compact('colors', 'sort_search'));
  149. }
  150. public function store_color(Request $request) {
  151. $request->validate([
  152. 'name' => 'required',
  153. 'code' => 'required|unique:colors|max:255',
  154. ]);
  155. $color = new Color;
  156. $color->name = Str::replace(' ', '', $request->name);
  157. $color->code = $request->code;
  158. $color->save();
  159. flash(translate('Color has been inserted successfully'))->success();
  160. return redirect()->route('colors');
  161. }
  162. public function edit_color(Request $request, $id)
  163. {
  164. $color = Color::findOrFail($id);
  165. return view('backend.product.color.edit', compact('color'));
  166. }
  167. /**
  168. * Update the color.
  169. *
  170. * @param \Illuminate\Http\Request $request
  171. * @param int $id
  172. * @return \Illuminate\Http\Response
  173. */
  174. public function update_color(Request $request, $id)
  175. {
  176. $color = Color::findOrFail($id);
  177. $request->validate([
  178. 'code' => 'required|unique:colors,code,'.$color->id,
  179. ]);
  180. $color->name = Str::replace(' ', '', $request->name);
  181. $color->code = $request->code;
  182. $color->save();
  183. flash(translate('Color has been updated successfully'))->success();
  184. return back();
  185. }
  186. public function destroy_color($id)
  187. {
  188. Color::destroy($id);
  189. flash(translate('Color has been deleted successfully'))->success();
  190. return redirect()->route('colors');
  191. }
  192. }