/**
  * Show the form for editing the specified resource.
  *
  * @param  int  $id
  * @return Response
  */
 public function edit($id)
 {
     $item = Inquiry::with('carinfo')->findOrFail($id);
     $cars = Car::lists('name', 'id')->all();
     $cities = City::lists('name', 'id')->all();
     foreach (config('vars.car_info.color') as $key => $value) {
         $colors[$key] = $value['name'];
     }
     return view('admin.inquiries.edit', compact('item', 'cars', 'cities', 'colors'));
 }
 public function homepage()
 {
     $user = Auth::user() ?: false;
     $cars = Car::with('inquiriesCount')->orderBy('domestic', 'asc')->orderBy('name')->get();
     $cars1 = $cars->filter(function ($item) {
         return $item->name == "ВАЗ";
     });
     $cars2 = $cars->filter(function ($item) {
         return $item->name != "ВАЗ";
     });
     $cars = $cars1->merge($cars2);
     $carsList = $cars->lists('name', 'id')->toArray();
     $lastInquiries = Inquiry::with('car', 'city')->orderBy('created_at', 'desc')->paginate(config('vars.inquiries_per_page'));
     $lastInquiries->setPath('inquiry/index');
     Carbon::setLocale(config('app.locale'));
     $lastNews = News::orderBy('published_at', 'desc')->paginate(config('vars.news_per_page'));
     $lastNews->setPath('news/index');
     $cities = City::lists('name', 'id')->all();
     $blocks = Block::all()->keyBy('alias');
     $faq = Faq::all();
     return view('homepage', compact('user', 'cars', 'carsList', 'lastInquiries', 'lastNews', 'cities', 'blocks', 'faq'));
 }
 /**
  * Update the specified resource in storage.
  *
  * @param  Request  $request
  * @param  int  $id
  * @return Response
  */
 public function update(Request $request, $id)
 {
     $rules = ['car_id' => 'required', 'city_id' => 'required', 'name' => 'required', 'phone' => 'required|min:7'];
     $messages = ['car_id.required' => 'Выберите марку автомобиля', 'city_id.required' => 'Выберите город', 'name.required' => 'Укажите свое имя', 'phone.required' => 'Укажите свой номер телефона', 'phone.min' => 'Укажите свой номер телефона'];
     $validator = Validator::make($request->all(), $rules, $messages);
     $validator->after(function ($validator) use($request) {
         if ($request->get('price_to') && $request->get('price_from') > $request->get('price_to')) {
             $validator->errors()->add('price_invalid', 'Значение "Цена, от" не может быть больше значения "Цена, до"');
         }
         if ($request->get('year_to') && $request->get('year_from') > $request->get('year_to')) {
             $validator->errors()->add('year_invalid', 'Значение "Год выпуска, от" не может быть больше значения "Год выпуска, до"');
         }
     });
     if ($validator->fails()) {
         $this->throwValidationException($request, $validator);
     }
     $item = Inquiry::findOrFail($id);
     $item->update($request->all());
     if ($request->has('carinfo')) {
         $carinfoFilled = false;
         foreach ($request->get('carinfo') as $value) {
             if ($value) {
                 $carinfoFilled = true;
                 break;
             }
         }
         if ($carinfoFilled) {
             $carinfo = Carinfo::where('inquiry_id', $id)->first() ?: new Carinfo();
             $carinfo->fill($request->get('carinfo'));
             $item->carinfo()->save($carinfo);
         } else {
             $item->carinfo()->delete();
         }
     }
     if ($request->ajax()) {
         $inquiry = Inquiry::with('car', 'carinfo', 'city')->findOrFail($item->id);
         return response()->json(['status' => 'success', 'message' => 'Объявление сохранено', 'inquiry' => $inquiry]);
     }
     Flash::success("Запись - {$id} обновлена");
     return redirect()->back();
 }