123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use App\Models\Attribute;
- use App\Models\Color;
- use App\Models\AttributeTranslation;
- use App\Models\AttributeValue;
- use CoreComponentRepository;
- use Str;
- class AttributeController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- CoreComponentRepository::instantiateShopRepository();
- CoreComponentRepository::initializeCache();
- $attributes = Attribute::orderBy('created_at', 'desc')->get();
- return view('backend.product.attribute.index', compact('attributes'));
- }
- /**
- * Show the form for creating a new resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function create()
- {
- }
- /**
- * Store a newly created resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- public function store(Request $request)
- {
- $attribute = new Attribute;
- $attribute->name = $request->name;
- $attribute->save();
- $attribute_translation = AttributeTranslation::firstOrNew(['lang' => env('DEFAULT_LANGUAGE'), 'attribute_id' => $attribute->id]);
- $attribute_translation->name = $request->name;
- $attribute_translation->save();
- flash(translate('Attribute has been inserted successfully'))->success();
- return redirect()->route('attributes.index');
- }
- /**
- * Display the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function show($id)
- {
- $data['attribute'] = Attribute::findOrFail($id);
- $data['all_attribute_values'] = AttributeValue::with('attribute')->where('attribute_id', $id)->get();
- // echo '<pre>';print_r($data['all_attribute_values']);die;
- return view("backend.product.attribute.attribute_value.index", $data);
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function edit(Request $request, $id)
- {
- $lang = $request->lang;
- $attribute = Attribute::findOrFail($id);
- return view('backend.product.attribute.edit', compact('attribute','lang'));
- }
- /**
- * Update the specified resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function update(Request $request, $id)
- {
- $attribute = Attribute::findOrFail($id);
- if($request->lang == env("DEFAULT_LANGUAGE")){
- $attribute->name = $request->name;
- }
- $attribute->save();
- $attribute_translation = AttributeTranslation::firstOrNew(['lang' => $request->lang, 'attribute_id' => $attribute->id]);
- $attribute_translation->name = $request->name;
- $attribute_translation->save();
- flash(translate('Attribute has been updated successfully'))->success();
- return back();
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function destroy($id)
- {
- $attribute = Attribute::findOrFail($id);
- foreach ($attribute->attribute_translations as $key => $attribute_translation) {
- $attribute_translation->delete();
- }
- Attribute::destroy($id);
- flash(translate('Attribute has been deleted successfully'))->success();
- return redirect()->route('attributes.index');
- }
- public function store_attribute_value(Request $request)
- {
- $attribute_value = new AttributeValue;
- $attribute_value->attribute_id = $request->attribute_id;
- $attribute_value->value = ucfirst($request->value);
- $attribute_value->save();
- flash(translate('Attribute value has been inserted successfully'))->success();
- return redirect()->route('attributes.show', $request->attribute_id);
- }
- public function edit_attribute_value(Request $request, $id)
- {
- $attribute_value = AttributeValue::findOrFail($id);
- return view("backend.product.attribute.attribute_value.edit", compact('attribute_value'));
- }
- public function update_attribute_value(Request $request, $id)
- {
- $attribute_value = AttributeValue::findOrFail($id);
-
- $attribute_value->attribute_id = $request->attribute_id;
- $attribute_value->value = ucfirst($request->value);
-
- $attribute_value->save();
- flash(translate('Attribute value has been updated successfully'))->success();
- return back();
- }
- public function destroy_attribute_value($id)
- {
- $attribute_values = AttributeValue::findOrFail($id);
- AttributeValue::destroy($id);
-
- flash(translate('Attribute value has been deleted successfully'))->success();
- return redirect()->route('attributes.show', $attribute_values->attribute_id);
- }
-
- public function colors(Request $request) {
- $sort_search = null;
- $colors = Color::orderBy('created_at', 'desc');
- if ($request->search != null){
- $colors = $colors->where('name', 'like', '%'.$request->search.'%');
- $sort_search = $request->search;
- }
- $colors = $colors->paginate(10);
- return view('backend.product.color.index', compact('colors', 'sort_search'));
- }
-
- public function store_color(Request $request) {
- $request->validate([
- 'name' => 'required',
- 'code' => 'required|unique:colors|max:255',
- ]);
- $color = new Color;
- $color->name = Str::replace(' ', '', $request->name);
- $color->code = $request->code;
-
- $color->save();
- flash(translate('Color has been inserted successfully'))->success();
- return redirect()->route('colors');
- }
-
- public function edit_color(Request $request, $id)
- {
- $color = Color::findOrFail($id);
- return view('backend.product.color.edit', compact('color'));
- }
- /**
- * Update the color.
- *
- * @param \Illuminate\Http\Request $request
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function update_color(Request $request, $id)
- {
- $color = Color::findOrFail($id);
- $request->validate([
- 'code' => 'required|unique:colors,code,'.$color->id,
- ]);
-
- $color->name = Str::replace(' ', '', $request->name);
- $color->code = $request->code;
-
- $color->save();
- flash(translate('Color has been updated successfully'))->success();
- return back();
- }
-
- public function destroy_color($id)
- {
- Color::destroy($id);
-
- flash(translate('Color has been deleted successfully'))->success();
- return redirect()->route('colors');
- }
-
- }
|