Example #1
0
 public function settings(Request $request)
 {
     if (Auth::check()) {
         User::edit($request);
         return view('profile.settings', ['categories' => Category::getAll(), 'regions' => Region::getAllWithCities()]);
     } else {
         return redirect('/');
     }
 }
Example #2
0
 /**
  * Display a listing of the resource.
  *
  * @return \Illuminate\Http\Response
  */
 public function index(AdFilters $filters, $slug = null, $make_slug = null, $model_slug = null)
 {
     if (Request::has('category')) {
         $getParameters = '?';
         foreach (Request::except(['category', 'make', 'model']) as $key => $value) {
             $getParameters .= $value == '' ? '' : "{$key}={$value}&";
         }
         $getParameters = substr($getParameters, 0, -1);
         $redirectPath = '/category/' . Request::input('category');
         if (Request::has('make') && Request::input('make') != 'any') {
             $redirectPath .= '/' . Request::input('make');
             if (Request::has('model') && Request::input('model') != 'any') {
                 $redirectPath .= '/' . Request::input('model');
             }
         }
         return redirect($redirectPath . $getParameters);
     }
     $builder = Ad::filter($filters)->orderBy('created_at', 'desc')->with(['auto_models', 'favorite']);
     if ($slug) {
         $category = Category::where('slug', $slug)->firstOrFail();
         if ($category->parent_id) {
             $builder->where('category_id', $category->id);
         } else {
             $categories_ids = $category->childs()->select('id')->get();
             $builder->whereIn('category_id', $categories_ids);
         }
         if ($make_slug) {
             if (!$model_slug) {
                 $make_id = AutoModel::where('slug', $make_slug)->select('id')->firstOrFail()->id;
                 $builder->whereHas('auto_models', function ($query) use($make_id) {
                     $query->where('auto_models.make_id', $make_id);
                 });
             } else {
                 $model_id = AutoModel::where('slug', $model_slug)->select('id')->firstOrFail()->id;
                 $builder->whereHas('auto_models', function ($query) use($model_id) {
                     $query->where('auto_models.id', $model_id);
                 });
             }
         }
     }
     $ads = $builder->paginate(30)->setPath(Request::url())->appends(Request::except('page'));
     // dd($slug, Category::where('slug', $slug)->first());
     $data = ['ads' => $ads, 'regions' => Region::getAllWithCities()];
     if (isset($category)) {
         $data['category'] = $category;
         $data['title'] = $category->name;
     }
     return view('mainpage', $data);
 }
Example #3
0
 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function update(StoreAdRequest $request, $id)
 {
     $ad = Ad::findOrFail($id);
     if (Gate::allows('update', $ad)) {
         $isFree = $request['is_free'] == 'on';
         $ad->update(['title' => $request['title'], 'text' => $request['text'], 'author_name' => $request['name'], 'phone' => $request['phone'], 'category_id' => $request['category_id'], 'city_id' => $request['city_id'], 'price' => $isFree ? 0 : $request['price'], 'is_free' => $isFree]);
         /*
          *   Store the ad's images
          */
         if ($request->has('images')) {
             $imagesArr = [];
             foreach ($request['images'] as $image) {
                 $imagePaths = explode('|', $image);
                 $imagesArr[] = ['ad_id' => $ad->id, 'small' => $imagePaths[0], 'big' => $imagePaths[1]];
             }
             AdImage::insert($imagesArr);
         }
         /*
          *   Update the ad's auto extra properties if received
          */
         if ($request->has('make')) {
             $autoExtraProperties = AutoExtraProperties::where('ad_id', $ad->id)->first();
             $autoExtraProperties->update(['model_id' => $request['model'], 'year' => $request['year'], 'capacity' => $request['capacity'], 'engine_type' => $request['engine_type'], 'transmission' => $request['transmission'], 'mileage' => $request['mileage'], 'power' => $request['power']]);
         }
         /*
          *   Update the ad's realty extra properties if received
          */
         if ($request->has('type_of_ad')) {
             $realtyExtraProperties = RealtyExtraProperties::where('ad_id', $ad->id)->first();
             $realtyExtraProperties->update(['type' => $request['type_of_ad'], 'lease' => $request['lease'], 'num_of_rooms' => $request['num_of_rooms'], 'square' => $request['square'], 'floor' => $request['floor'], 'num_of_floors' => $request['num_of_floors']]);
         }
         return view('publish', ['categories' => Category::getAll(), 'regions' => Region::getAllWithCities(), 'ad' => $ad, 'updated' => true]);
     } else {
         return redirect('/');
     }
 }