Example #1
0
 public function postLikePost(Request $request)
 {
     $post_id = $request['postId'];
     $isLike = $request['isLike'] === 'true';
     // ? true  : false;
     $update = false;
     $post = Post::find($post_id);
     if (!$post) {
         return null;
     }
     $user = Auth::user();
     $like = $user->likes()->where('post_id', $post_id)->first();
     if ($like) {
         $alreadyLike = $like->like;
         $update = true;
         if ($alreadyLike == $isLike) {
             $like->delete();
             return null;
         }
     } else {
         $like = new Like();
     }
     $like->like = $isLike;
     $like->user_id = $user->id;
     $like->post_id = $post->id;
     if ($update) {
         $like->update();
     } else {
         $like->save();
     }
     return null;
 }
 public function like($domain, $productId)
 {
     $product = Product::find($productId);
     $product->rating += 5;
     $product->save();
     $like = $product->likes()->where('liker_id', $this->user->id)->first();
     if ($like) {
         return $this->responseBadRequest('You already liked this one');
     } else {
         $like = new Like();
         $like->product_id = $productId;
         $like->liker_id = $this->user->id;
         $like->save();
         if ($this->user->id != $product->author->id) {
             $notification = new Notification();
             $notification->product_id = $productId;
             $notification->actor_id = $this->user->id;
             $notification->receiver_id = $product->author->id;
             $notification->type = 0;
             $notification->save();
             $publish_data = array("event" => "notification", "data" => ["notification" => $this->notificationTransformer->transform($notification)]);
             Redis::publish('colorme-channel', json_encode($publish_data));
         }
         send_push_notification(json_encode($publish_data));
         return $this->respond(['product_id' => $productId, 'like_counts' => $product->likes()->count()]);
     }
 }
Example #3
0
 public function like($likeable)
 {
     if ($this->isLiked($likeable)) {
         throw new \Exception();
     }
     $like = new Like();
     $like->user()->associate($this);
     $likeable->likes()->save($like);
     return $this;
 }
 public function postLikepost()
 {
     $data = Input::all();
     $rules = ["post_id" => 'required|exists:posts,id'];
     $validator = Validator::make($data, $rules);
     if ($validator->fails()) {
         return 404;
     } else {
         $check_like = Like::check_like(Auth::user()->id, $data['post_id']);
         if ($check_like) {
             $unlike = Like::unlike(Auth::user()->id, $data['post_id']);
             if ($unlike) {
                 $total_like = Like::total_like($data['post_id']);
                 $total_like_text = $total_like ? $total_like . " people like this" : "";
                 return json_encode(["like" => "Like", "total_like" => $total_like_text]);
             } else {
                 return 404;
             }
         } else {
             $like = Like::like(Auth::user()->id, $data['post_id']);
             if ($like) {
                 $total_like = Like::total_like($data['post_id']);
                 $total_like_text = $total_like ? $total_like . " people like this" : "";
                 return json_encode(["like" => "Unlike", "total_like" => $total_like_text]);
             } else {
                 return 404;
             }
         }
     }
 }
 /**
  * Show the form for creating a new resource.
  *
  * @return \Illuminate\Http\Response
  */
 public function liked()
 {
     $forms = Form::all();
     $comments = Comment::all();
     $likes = Like::all();
     return view('user.likes', compact('forms', 'comments', 'likes'));
 }
 /**
  * Bootstrap any application services.
  *
  * @return void
  */
 public function boot()
 {
     Like::creating(function ($attribute) {
         // -1 like_left for media
         $media = Media::findOrFail($attribute->media_id);
         $media->likes_left = $media->likes_left - 1;
         $media->save();
         // +1 credit for liker of media
         $user = User::findOrFail($attribute->user_id);
         if ($user->pro_user) {
             $user->credit = $user->credit + 3;
             //for pro user
         } else {
             $user->credit = $user->credit + 1;
         }
         $user->save();
     });
     Follow::creating(function ($attribute) {
         // -1 like_left for media
         $user = User::findOrFail($attribute->follow_id);
         $user->followers_left = $user->followers_left - 1;
         $user->save();
         // +1 credit for liker of media
         $user = User::findOrFail($attribute->user_id);
         if ($user->pro_user) {
             $user->credit = $user->credit + 3;
             //for pro user
         } else {
             $user->credit = $user->credit + 1;
         }
         $user->save();
     });
 }
 public function like(Request $request)
 {
     onlyAllowPostRequest($request);
     $post_id = $request->input('id');
     $user_id = $request->input('user');
     /**
      * Dữ liệu trả về
      */
     $response = new stdClass();
     $likes = Like::all()->where('user_id', intval($user_id))->where('post_id', intval($post_id));
     if ($likes->count() > 0) {
         $response->error = true;
         $response->error_msg = 'Bạn đã cảm ơn bài viết này!';
         return response()->json($response);
     }
     $like = Like::create(['user_id' => $user_id, 'post_id' => $post_id]);
     $posts = Post::all()->where('id', intval($post_id));
     if ($posts->count() == 0) {
         $response->error = true;
         $response->error_msg = 'Bạn đã cảm ơn bài viết này!';
         return response()->json($response);
     }
     $count_like = intval($posts->first()->like);
     $count_like++;
     $p = DB::table('posts')->where('id', intval($post_id))->update(['like' => $count_like]);
     $response->error = false;
     $response->msg = 'Cảm ơn bạn!';
     return response()->json($response);
 }
Example #8
0
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function destroy($id)
 {
     $like = Like::find($id);
     $post = Post::find($like->post_id);
     Like::destroy($id);
     return redirect('users');
 }
Example #9
0
 public function likable()
 {
     $user_id = User::getCurrentUserId();
     //Check if the media was already liked by the user
     $like = Like::where('media_id', $this->id)->where('user_id', $user_id)->first();
     return !(bool) $like;
 }
Example #10
0
 public function destroy($id)
 {
     //
     //$like = Like::find($id);
     //	$user = User::find($like->user_id);
     Like::destroy($id);
     return Redirect::back();
 }
Example #11
0
 /**
  * Total like of user
  * @author  Tran Van Moi
  * @since  2015/05/21
  * @param  int $user_id
  * @return  int
  */
 public static function total_like_user($user_id)
 {
     $posts = Post::whereUser_id($user_id)->get();
     $arr_post = [];
     foreach ($posts as $post) {
         $arr_post[] = $post['id'];
     }
     return Like::whereIn('post_id', $arr_post)->count();
 }
 public function setLike(Request $request)
 {
     $data = Input::get('photoId');
     $data = intval($data);
     $ip = $request->getClientIp();
     $validation = DB::table('likes')->where('idDraw', $data)->where('ip', $ip)->count();
     if ($validation == 0) {
         $like = new Like();
         $like->ip = $ip;
         $like->idDraw = $data;
         $like->save();
         DB::table('images')->where('id', $data)->increment('likes', 1);
         $message = 'Liked';
     } else {
         $message = 'You already liked this drawing';
     }
     echo json_encode($message);
 }
 public function postLike(\Illuminate\Http\Request $request)
 {
     $id = e($request->input('id'));
     $course = Course::findOrFail($id);
     if (Like::where('course', '=', $id)->where('user', '=', \Auth::user()->id)->exists()) {
         $course->likes--;
         $like = Like::where('course', '=', $id)->where('user', '=', \Auth::user()->id)->first();
         $like->delete();
     } else {
         $course->likes++;
         $like = new Like();
         $like->course = $id;
         $like->user = Auth::user()->id;
         $like->save();
     }
     $course->save();
     return view::make('courses.liked', compact('course'));
 }
Example #14
0
 public function index()
 {
     $totallikes = Like::count();
     $totalposts = Post::count();
     $posts = Post::with('cat', 'likes')->paginate(config('app.posts_per_page'));
     $page = $posts->currentPage();
     $lastpage = $posts->lastPage();
     return view('index', compact('totallikes', 'totalposts', 'posts', 'page', 'lastpage', 'title'));
 }
Example #15
0
 public function viewReadAll()
 {
     // Set logged in user to a variable.
     $authUser = Auth::user();
     // Find user's likes in database.
     $likes = Like::where('user_id', '=', $authUser->id)->latest('id')->get();
     // Find all photos in the database.
     $photos = Photo::all();
     // Return view with variables.
     return view('likes.viewReadAll')->with('likes', $likes)->with('authUser', $authUser)->with('photos', $photos);
 }
Example #16
0
 public function removeLike($entry_id)
 {
     $user = JWTAuth::parseToken()->authenticate();
     $newLike['entry_id'] = $entry_id;
     $newLike['user_id'] = $user->id;
     if (Like::where(['entry_id' => $newLike['entry_id'], 'user_id' => $newLike['user_id']])->exists()) {
         Like::where(['entry_id' => $newLike['entry_id'], 'user_id' => $newLike['user_id']])->delete();
     } else {
         abort(403, 'Like is not registered');
     }
 }
Example #17
0
 /**
  * Handle an incoming request.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  \Closure  $next
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
     $album = $request->album;
     $albumid = $request->album->id;
     $like = Like::SelectLikedAlbum($albumid)->SelectUser(Auth::id())->get();
     if ($like == '[]') {
         return view('albums.show.unliked')->with('album', $album);
     } else {
         return view('albums.show.liked')->with('album', $album);
     }
 }
Example #18
0
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     $user = new User();
     $user->name = 'hisyam';
     $user->email = '*****@*****.**';
     //$user->photo = 'southern_miss_logo.0.0.jpeg';
     $user->password = bcrypt('admin1');
     $user->save();
     $stat = Stat::create(['user_id' => '1', 'content' => 'Lorem Ipsum', 'published_at' => Carbon\Carbon::now()]);
     $like = Like::create(['stat_id' => '1', 'count' => 45]);
     $comment = Comment::create(['stat_id' => 1, 'user_id' => 1, 'content' => 'Lorem Ipsum', 'published_at' => Carbon\Carbon::now()]);
 }
Example #19
0
 public function getLikeComment(Request $request, $slug, $comment)
 {
     $user = User::where('id', $request->user()->id)->first();
     $comment = Comment::where('id', $comment)->first();
     if ($user && $comment) {
         if ($user->id == $comment->user_id) {
             return redirect()->route('home')->withError('Ошибка, хакер что ли?');
         }
         if ($user->hasLikedComment($comment, $request->user()->id)) {
             return redirect()->back()->withError('Вы уже это лайкнули.');
         }
         $like = new Like();
         $like->user_id = $user->id;
         $like->content_id = $comment->id;
         $like->content_type = 'App\\Comment';
         $like->save();
         return redirect()->back()->withMessage('Спасибо за лайк.');
     } else {
         return redirect()->route('home')->withError('Ошибка, свяжитесь с администратором.');
     }
 }
 /**
  * Remove the specified resource from storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function destroy(Request $request, $id)
 {
     $like = Like::find($id);
     if (empty($story)) {
         abort(404);
     }
     if ($request->user()->id !== $like->user_id) {
         abort(403);
     }
     $like->delete();
     $like->likeable->updateLikeCount();
 }
 /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     //Get the Media from given URL
     $media = Media::where('url', $request->url)->first();
     if (!$media) {
         return response()->json(['success' => false, 'message' => "No such media in our DB"]);
     }
     if (!$media->publishable) {
         return response()->json(['success' => false, 'message' => "You cannot like this media"]);
     }
     return response()->json(['success' => true, 'message' => "Successfully liked", 'like' => Like::create(['user_id' => User::getCurrentUserId(), 'media_id' => $media->id])]);
 }
Example #22
0
 public function storeLike(Request $request, $name)
 {
     $data = $request->all();
     if ($data['like'] == "true") {
         $data['like'] = true;
     } else {
         $data['like'] = false;
     }
     $data['user_id'] = Auth::user()->id;
     //CHECK WHETHER LIKED OR DISLIKED AND REACT ACCORDINGLY
     $likeObject = new Like();
     $isLike = $likeObject->isLike($data['user_id'], $data['post_id']);
     $isDislike = $likeObject->isDislike($data['user_id'], $data['post_id']);
     if (!$isLike && !$isDislike) {
         //if not liked and not disliked
         Like::create($data);
     } elseif ($isLike && !$isDislike && $data['like'] == true) {
         //already liked,not disliked,pressed like button
         //dd("already liked,not disliked,pressed like button");
         Like::where(['post_id' => $data['post_id'], 'user_id' => $data['user_id']])->delete();
     } elseif ($isLike && !$isDislike && $data['like'] == false) {
         //already liked,not disliked,but pressed dislike button
         //dd("already liked,not disliked,but pressed dislike button");
         Like::where(['post_id' => $data['post_id'], 'user_id' => $data['user_id']])->update(array('like' => false));
     } elseif (!$isLike && $isDislike && $data['like'] == true) {
         //not liked,already disliked,but pressed like button
         //dd("not liked,already disliked,but pressed like button");
         Like::where(['post_id' => $data['post_id'], 'user_id' => $data['user_id']])->update(array('like' => true));
     } elseif (!$isLike && $isDislike && $data['like'] == false) {
         //not liked,already disliked,but pressed dislike button
         //dd("not liked,already disliked,but pressed dislike button");
         Like::where(['post_id' => $data['post_id'], 'user_id' => $data['user_id']])->delete();
     }
     $post = Post::find($data['post_id']);
     $countLikeDislike = null;
     $countLikeDislike['likes'] = $post->Likes()->count();
     $countLikeDislike['dislikes'] = $post->Dislikes()->count();
     $countLikeDislike['icon_flag'] = $data['like'];
     return $countLikeDislike;
 }
Example #23
0
 public function getEntries($challengeType, $challenge_id)
 {
     try {
         $userToken = JWTAuth::parseToken()->authenticate();
         $signed_in = true;
     } catch (\Exception $e) {
         $signed_in = false;
     }
     $challenge = Challenge::find($challenge_id);
     $entries = $challenge->entries;
     if ($entries) {
         foreach ($entries as $entry) {
             $user = $entry->user;
             unset($entry->challenge_id);
             unset($entry->created_at);
             unset($entry->updated_at);
             unset($entry->user);
             $entry->userPic = $user->userPic;
             $entry->userName = $user->name;
             $entry->category = $challenge->isWeekly ? 'weekly' : 'monthly';
             $entry->likes = $entry->likesCount();
             if ($signed_in) {
                 $likes = Like::where('user_id', $userToken->id)->get();
                 foreach ($likes as $like) {
                     if ($like->entry_id == $entry->id) {
                         $entry->liked = true;
                     } else {
                         $entry->liked = false;
                     }
                 }
             }
         }
         return $challenge;
     } else {
         return false;
     }
 }
 /**
  * View profile of other user function
  *
  * @author Tran Van Moi <[moitran92@gmail.com]>
  * @since 2015/05/12
  * @return Response
  */
 public function getView($user_id = null, $category_id = null)
 {
     $keyword = trim(Input::get('keyword'));
     $category_id = trim($category_id);
     $data = ['user_id' => $user_id];
     $rules = ['user_id' => 'required|exists:users,id'];
     $validator = Validator::make($data, $rules);
     if ($validator->fails()) {
         Session::flash('view_user_error', 'User is not found!');
     } else {
         if ($user_id == Auth::user()->id) {
             return redirect('user/profile');
         }
         $check_user_exist = User::whereDelete_status(0)->whereId($user_id)->first();
         if ($check_user_exist) {
             $data['keyword'] = $keyword;
             $rules = ['keyword' => 'max:150|min:1'];
             $validator = Validator::make($data, $rules);
             if ($validator->fails()) {
                 $data['keyword'] = "";
                 $data['posts'] = Post::get_all_posts($category_id, "", $user_id);
             } else {
                 $data['posts'] = Post::get_all_posts($category_id, $keyword, $user_id);
             }
             $data['categories'] = Category::all();
             if (filter_var($check_user_exist->image, FILTER_VALIDATE_URL) === false) {
                 $check_user_exist->image = URL::to('/') . "/public/images/avatar/" . $check_user_exist->image;
             }
             $data['user_info'] = $check_user_exist;
             $data['total_like'] = Like::total_like_user($user_id);
         } else {
             Session::flash('view_user_error', 'User is not found!');
         }
     }
     return view('user/view', $data);
 }
Example #25
0
 /**
  * Toggle tattoo like
  *
  * @return View
  */
 public function like(Request $request)
 {
     $res = array();
     $res['success'] = false;
     $tattooId = $request->input('id');
     $user = Auth::user();
     if ($user->likes()->where('tattoo_id', $tattooId)->count()) {
         $like = Like::where('tattoo_id', $tattooId)->where('user_id', $user->id)->first();
         if ($like) {
             //already liked so unlike now(delete)
             $like->delete();
             $res['data'] = 'Like';
             //unlike success now show Like
             $res['success'] = true;
             $res['likeCount'] = Like::where('tattoo_id', $tattooId)->count();
         }
     } else {
         $like = new Like();
         $like->tattoo_id = $tattooId;
         $like->user_id = $user->id;
         if ($like->save()) {
             $res['data'] = 'Unlike';
             //like success now show Unlike
             $res['success'] = true;
             $res['likeCount'] = Like::where('tattoo_id', $tattooId)->count();
         }
     }
     return $res;
 }
Example #26
0
 /**
  * Display a listing of the resource.
  *
  * @return \Illuminate\Http\Response
  */
 public function index()
 {
     $post_all = Post::latest()->published()->paginate(5);
     //get one to many
     $post = Post::find(11);
     if ($post) {
         $comments = $post->comments()->get();
     }
     //$comments->posts()->where('active', 1)->get();
     //$comments = Post::find(11)->comments()->where('title', 'foo')->first();
     //inverse
     $comment = Comment::find(1);
     if ($comment) {
         $post_found = $comment->post->title;
     }
     //many to many
     $user = User::find(32);
     if ($user) {
         $roles = $user->roles;
     }
     //$roles2 = User::find(11)->roles()->orderBy('role')->get();
     //Has Many Through
     $country = Country::find(1);
     $post_by_country = $country->posts;
     //Polymorphic
     //foreach ($post->likes as $like) {}
     //inverse
     $like = Like::find(1);
     if ($like) {
         $likeable = $like->likeable;
     }
     //Many To Many Polymorphic
     //$post = App\Post::find(1);
     //foreach ($post->tags as $tag) {}
     //inverse
     $tag = Tag::find(1);
     //Querying Relationship Existence
     // Retrieve all posts that have at least one comment...
     //$posts = Post::has('comments')->get();
     // Retrieve all posts that have three or more comments...
     $posts = Post::has('comments', '>=', 3)->get();
     // Retrieve all posts that have at least one comment with votes...
     //$posts = Post::has('comments.votes')->get();
     // Retrieve all posts with at least one comment containing words like foo%
     // $posts = Post::whereHas('comments', function ($query) {//orWhereHas
     //     $query->where('content', 'like', 'foo%');
     // })->get();
     //Eager loading
     $post2 = Post::with('user')->get();
     //Eager Loading Multiple Relationships
     //Sometimes you may need to eager load several different relationships in a single operation. To do so, just pass additional arguments to the with method:
     //$books = Post::with('user', 'publisher')->get();
     //Nested Eager Loading
     //To eager load nested relationships, you may use "dot" syntax. For example, let's eager load all of the book's authors and all of the author's personal contacts in one Eloquent statement:
     //$books = App\Book::with('author.contacts')->get();
     //Constraining Eager Loads
     //Sometimes you may wish to eager load a relationship, but also specify additional query constraints for the eager loading query. Here's an example:
     // $users = App\User::with(['posts' => function ($query) {
     //     $query->where('title', 'like', '%first%');
     // }])->get();
     //In this example, Eloquent will only eager load posts that if the post's title column contains the word first. Of course, you may call other query builder to further customize the eager loading operation:
     // $users = App\User::with(['posts' => function ($query) {
     //     $query->orderBy('created_at', 'desc');
     // }])->get();
     //Lazy Eager Loading
     //Sometimes you may need to eager load a relationship after the parent model has already been retrieved. For example, this may be useful if you need to dynamically decide whether to load related models:
     //$books = App\Book::all();
     // if ($someCondition) {
     //     $books->load('author', 'publisher');
     // }
     //If you need to set additional query constraints on the eager loading query, you may pass a Closure to the load method:
     // $books->load(['author' => function ($query) {
     //     $query->orderBy('published_date', 'asc');
     // }]);
     return view('posts.index', compact('post', 'comments', 'post_found', 'user', 'roles', 'roles2', 'country', 'post_by_country', 'likeable', 'tag', 'post2', 'post_all'));
 }
 public function random()
 {
     $form = Form::all();
     $first = $form->first();
     $last = $form->last();
     do {
         $id = mt_rand($first->id, $last->id);
         $forms = Form::find($id);
     } while (!$forms);
     Form::find($forms->id)->increment('views');
     $comments = Comment::all();
     $users = User::all();
     $user = \Auth::user();
     $likes = Like::all();
     foreach ($users as $formuser) {
         if ($forms->user_id == $formuser->id) {
             $username = $formuser->name;
             $userlastname = $formuser->lastname;
             $userid = $formuser->id;
         }
     }
     $likedata = $this->countLikes($likes, $forms);
     $likesamount = $likedata['likesamount'];
     $likesis = $likedata['likesis'];
     return view('form.show', compact('forms'), compact('comments', 'likesis', 'likesamount', 'user', 'username', 'userlastname', 'userid'));
 }
Example #28
0
 public function userLike(User $user)
 {
     $like = Like::where('user_id', $user->id)->where('post_id', $this->id)->first();
     return $like;
 }
 public static function hasLiked($iFromWhereID, $iToUserID, $iUserID, $iLikeFromType, $iLikeFromID)
 {
     $oResult = Like::where($iFromWhereID, $iLikeFromID)->where('to_user_id', $iToUserID)->where('from_user_id', $iUserID)->where('status', Like::LIKE_STATUS_ACTIVE)->get();
     if ($oResult->first()) {
         return $oResult->first()->toArray();
     } else {
         return false;
     }
 }
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function destroy($id)
 {
     $like = Like::find($id);
     Like::destroy($id);
     return redirect()->back();
 }