public function follow() { //Checks if user is logged in if (Auth::check()) { //checks if ajax request if (Request::ajax()) { $post = Input::get('post'); $user = Auth::user()->id; //checks if user is already following post $follow = DB::table('follows')->where('user_id', $user)->where('post_id', $post); //if row exists if ($follow->count() > 0) { //remove row $follow->delete(); } else { //add new row Follow::create(['user_id' => $user, 'post_id' => $post]); } } else { return "No Ajax Request"; } } else { return "Not logged in"; } }
public function run() { DB::table('follows')->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; Follow::create(array('id' => '1', 'from_id' => $user_id, 'to_id' => $user_1)); Follow::create(array('id' => '2', 'from_id' => $user_id, 'to_id' => $user_2)); }
public function add() { redirect_guest_user(LOGIN_URL); $thread = Thread::get(Param::get('id')); $auth_user = User::getAuthenticated(); if (!$auth_user) { die; } $follow = new Follow(); $follow->thread_id = $thread->id; $follow->user_id = $auth_user->id; $follow->create(); redirect(VIEW_THREAD_URL, array('id' => $follow->thread_id)); }
public function create() { redirect_guest_user(LOGIN_URL); $thread = new Thread(); $comment = new Comment(); $page = Param::get('page_next', 'create'); $auth_user = User::getAuthenticated(); $categories = Category::getAll(); switch ($page) { case 'create': break; case 'create_end': $thread->title = trim_collapse(Param::get('title')); $thread->category_id = Param::get('category'); $thread->user_id = $auth_user->id; $comment->user_id = $auth_user->id; $comment->body = trim(Param::get('body')); $db = DB::conn(); try { $db->begin(); $thread->create($comment); $follow = new Follow(); $follow->thread_id = $thread->id; $follow->user_id = $auth_user->id; $follow->last_comment = Comment::getFirstInThread($thread)->id; $follow->create(); $db->commit(); redirect(VIEW_THREAD_URL, array('id' => $thread->id)); } catch (ValidationException $e) { $page = 'create'; $db->rollback(); } catch (CategoryException $e) { $thread->validation_errors['category']['exists'] = true; $page = 'create'; $db->rollback(); } break; default: throw new PageNotFoundException("{$page} is not found"); break; } $title = 'Create Thread'; $this->set(get_defined_vars()); $this->render($page); }