1. PHP / Говнокод #21719

    −27

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    public function product(Request $request, $id = null)
        {
            $this->template['product'] = $id ? Product::find($id) : new Product();
            $this->template['product'] || abort(404);
            if(!$this->template['product']->exists && count($input)){
                if(array_key_exists('category', $input) && $input['category']){
                    $category = Category::where('alias','=',$input['category'])->first();
                    if($category){
                        $this->template['product']->category_id = $category->id;
                    }
                }
                if(array_key_exists('brand', $input) && $input['brand']){
                    $car = Car::where('alias','=', $input['brand'])->first();
                    if($car){
                        $this->template['product']->car_id = $car->id;
                    }
                }
                if(array_key_exists('model', $input) && $input['model']){
                    $car_model = CarModel::where('alias','=',$input['model'])->first();
                    if($car_model){
                        $this->template['product']->car_model_id = $car_model->id;
                    }
                }
                if(array_key_exists('year', $input) && $input['year']){
                    $this->template
                }
            }
    
            $this->template['categories'] = Category::all();
            $this->template['brands']     = Brand::all();
            $this->template['years']      = Year::all()->sortBy('year');
            $this->template['cars']       = Car::all();
            $this->template['car_models'] = CarModel::all();
    
    
            return View::make('dashboard.product', $this->template);
        }

    Не знаю наверное у Вас так не принято) но подскажите как избавиться от говноёлки?!)

    Запостил: Vilintritenmert, 24 Ноября 2016

    Комментарии (3) RSS

    • $input = $request->input();
      if (!$this->template['product']->exists && count($input)) {
      if (array_key_exists('category', $input) && $input['category'] && $category = Category::whereAlias($input['category'])->first()) {
      $this->template['product']->category_id = $category->id;
      }
      if (array_key_exists('brand', $input) && $input['brand'] && $car = Car::whereAlias($input['brand'])->first()) {
      $this->template['product']->car_id = $car->id;
      }
      if (array_key_exists('model', $input) && $input['model'] && $car_model = CarModel::whereAlias($input['model'])->first()) {
      $this->template['product']->car_model_id = $car_model->id;
      }
      }

      где лопата?
      еще может проще вариант?
      Ответить
    • Опишите декларативно зависимость модели от поля (CarModel -- model), а потом вот так (это не php, это псевдосинтаксис чтобы показать идею)

      for ($key => $value in input) {
        $modelClass = getModelClass($key);
       $this->template["product"][$key] =  $modelClass->where($key, "=", $value);
      }
      
      // Внутри getModelClass ассоциативный массив где ключ "model" а значение -- класс CarModel итд
      Ответить
      • анус твой принял ассоциативный массив чурекских хуев, проверь, и поверь
        Ответить

    Добавить комментарий