public function discover() { if (Auth::check()) { $requests = Auth::user()->friendRequest(); $posts = Posts::notReply()->where(function ($query) { return $query->where('isPublic', true)->lists('id'); })->orderBy('created_at', 'desc')->paginate(10); return view('discover')->with('posts', $posts)->with('requests', $requests); } }
public function getProfile($id) { $user = User::where('id', $id)->first(); $requests = Auth::user()->friendRequest(); if (!$user) { abort(404); } $posts = Posts::notReply()->where('user_id', $id)->orderBy('created_at', 'desc')->paginate(10); $posts_pub = Posts::notReply()->where('user_id', $id)->where('isPublic', true)->orderBy('created_at', 'desc')->paginate(10); /* * $posts_pub= Posts::notReply()->where('user_id',$id)->orWhere(function($query){ $query->where('isPublic',true);})->orderBy('created_at', 'desc') ->paginate(10); * */ //dd($posts); return view('profile.index')->with('user', $user)->with('posts', $posts)->with('requests', $requests)->with('posts_pub', $posts_pub)->with('authUserIsFriend', Auth::user()->isFriendWith($user)); }
public function getLike($postsId) { $posts = Posts::find($postsId); if (!$posts) { return redirect()->route('home'); } if (!Auth::user()->isFriendWith($posts->user)) { return redirect()->route('home'); } if (Auth::user()->hasLikedPosts($posts)) { $posts->likes()->where('user_id', Auth::id())->first()->delete(); return response()->json(['number_of_likes' => $posts->likes->count()]); // return redirect()->back(); } $like = $posts->likes()->create([]); Auth::user()->likes()->save($like); $number_of_likes = $posts->likes->count(); event(new PostWasLiked(Auth::user()->getName(), $posts->id, $number_of_likes)); return response()->json(['number_of_likes' => $number_of_likes]); // return redirect()->back(); }
public function hasLikedPosts(Posts $posts) { return (bool) $posts->likes()->where('likeable_id', $posts->id)->where('likeable_type', get_class($posts))->where('user_id', $this->id)->count(); }
/** * Get the channels the event should be broadcast on. * * @return array */ public function broadcastOn() { $user_id = Posts::find($this->post_id)->user_id; return ['user.' . $user_id]; }