/** * Store a newly created resource in storage. * * @return Response */ public function store(LikeRequest $request) { $input = $request->all(); if (Auth::guest()) { \Session::flash('flash_message', 'You need to be logged in to like/dislike!'); } else { $input['user_id'] = Auth::id(); Like::create($input); } $video = Video::findOrFail($input['video_id']); return view('videos/show', compact('video')); }
public function run() { DB::table('likes')->delete(); $user_id = User::where('email', '*****@*****.**')->first()->user_id; $user_1 = User::where('email', '*****@*****.**')->first()->user_id; $user_2 = User::where('email', '*****@*****.**')->first()->user_id; Like::create(array('id' => '1', 'rating_id' => '1', 'user_id' => $user_id)); Like::create(array('id' => '2', 'rating_id' => '2', 'user_id' => $user_id)); Like::create(array('id' => '3', 'rating_id' => '2', 'user_id' => $user_1)); Like::create(array('id' => '4', 'rating_id' => '3', 'user_id' => $user_1)); Like::create(array('id' => '5', 'rating_id' => '2', 'user_id' => $user_2)); Like::create(array('id' => '6', 'rating_id' => '1', 'user_id' => $user_2)); }
private function storeOrGet($type, $id) { $user = Auth::user(); $like = $this->get($type, $id, $user->id); if (!$like) { $like = Like::create(['likeable_id' => $id, 'likeable_type' => $type, 'user_id' => $user->id, 'created_at' => Carbon\Carbon::now()]); if (in_array($type, array('Post', 'Comment'))) { $object = $type::find($id); if ($object) { if ($user->id != $object->user_id && ($device = Device::where('user_id', $object->user_id)->get()->first())) { $method = "set{$type}AsLiked"; $state = new StateSender($device->auth_token); $state->{$method}($object, Auth::user()); } } } } return $like; }
public function voteComment() { $value = Input::get('value'); $comment_id = Input::get('comment_id'); $user_id = Auth::user()->id; $consecutive_same_vote = false; $comment = Comment::find($comment_id); //we don't want user making more then 1 vote up or down. //if vote is up and already up vote, then vote is converted to down and vice versa if ($value == 1) { $positive_likes = $comment->likes()->where('user_id', '=', $user_id)->where('value', '=', '1')->get(); if ($positive_likes->count()) { $positive_likes->first()->delete(); $consecutive_same_vote = true; } } else { if ($value == -1) { $negative_likes = $comment->likes()->where('user_id', '=', $user_id)->where('value', '=', '-1')->get(); if ($negative_likes->count()) { $negative_likes->first()->delete(); $consecutive_same_vote = true; } } } if (!$consecutive_same_vote) { $like = Like::create(array('value' => $value, 'user_id' => $user_id, 'comment_id' => $comment_id)); $like->save(); } //calculate rank $rank = $comment->likes()->sum('value'); $comment->rank = $rank; $comment->save(); if ($value == -1) { $count = $comment->likes()->where('value', '=', '-1')->get()->count(); } else { $count = $comment->likes()->where('value', '=', '1')->get()->count(); } //email notification if ($comment->user->rank_notice) { if ($value == -1) { $vote = " up "; } else { $vote = " down "; } $message = "You're comment has been voted"; $message .= $vote . "!\n "; if ($comment->parent_id != 0) { $parent_comment = $comment->parent; } else { $parent_comment = $comment; } $message .= str_replace(' ', '%20', 'http://iratepolitics.com/politicians/' . $parent_comment->politician->full_name . '/#' . $parent_comment->user->username . '-' . $parent_comment->id); $message .= "\n\n\nTo disable comment response, uncheck 'Receive email notice of up/down votes on my comments.' here: "; $message .= URL::route('edit-myprofile'); $subject = "Someone has voted" . $vote . "your comment!"; // message lines should not exceed 70 characters (PHP rule), so wrap it $message = wordwrap($message, 70); mail($parent_comment->user->email, $subject, $message, "From: contact@iratepolitics.com\n"); } //return $count;//rand(1,10); //from old arangement counting likes up and down return $rank; //new arrangement returning comment rank }