public function postvote(Request $request) { if (!$request->ajax()) { return back(); } $ip = $request->getClientIp(); $binip = inet_pton($ip); $hexip = bin2hex($binip); /*$hexip = bin2hex(inet_pton('62.57.188.225'));*/ if (Postvote::whereRaw("post_id = ? AND HEX(ip_address) = ?", array($request->post_id, $hexip))->count() > 0) { return response()->json(['state' => false, 'message' => 'Ya se ha votado desde esta ip']); } $user_id = Auth::user() ? Auth::user()->id : null; $postvote = new PostVote(); $postvote->post_id = $request->post_id; $postvote->user_id = $user_id; $postvote->ip_address = $binip; $postvote->save(); $post = Post::find($request->post_id); $count = count($post->postvotes); $post->up = $count; $post->save(); return response()->json(['state' => true, 'count' => $count]); }
public function update($id, Request $request) { $rules = $this->rules($request->all()); if ($rules['input-multi-dates-format'] == 'error') { return Redirect::back()->withInput()->withErrors('hola'); } if ($request->input('datestype') == 'single-date') { $datestype = 1; } elseif ($request->input('datestype') == 'interval-dates') { $datestype = 2; } elseif ($request->input('datestype') == 'multi-dates') { $datestype = 3; } else { return Redirect::back()->withInput()->withErrors('hola'); } $this->validate($request, $rules); /*cambia con store*/ $post = Post::find($id); $post->title = $request->input('title'); $post->location = $request->input('location'); $post->content = $request->input('content'); $post->lat = $request->input('lat'); $post->lng = $request->input('lng'); $post->url = $request->input('url'); $post->datestype = $datestype; $post->user_id = Auth::user()->id; $post->save(); /*cambia con store*/ Date::where('post_id', $post->id)->delete(); if ($datestype == 1) { $date = new Date(); $date->post_id = $post->id; $datetime = DateTime::createFromFormat('d/m/y', $request->input('input-single-date')); $date->date = $datetime->format('Y-m-d'); $date->save(); } elseif ($datestype == 2) { $date = new Date(); $datetime = DateTime::createFromFormat('d/m/y', $request->input('input-from-date')); $date->post_id = $post->id; $date->date = $datetime->format('Y-m-d'); $date->save(); $date = new Date(); $datetime = DateTime::createFromFormat('d/m/y', $request->input('input-to-date')); $date->post_id = $post->id; $date->date = $datetime->format('Y-m-d'); $date->save(); } elseif ($datestype == 3) { $dates = array_map('trim', explode(',', $request->input('input-multi-dates'))); foreach ($dates as $key => $val) { $date = new Date(); $datetime = DateTime::createFromFormat('d/m/y', $val); $date->post_id = $post->id; $date->date = $datetime->format('Y-m-d'); $date->save(); } } if ($request->input('tags')) { foreach ($request->input('tags') as $tagname) { $tag = Tag::create(['name' => $tagname]); $post->tags()->attach($tag); } } return Redirect::route('single', [$post->id, $post->title]); }