/** * get_show takes in a username, finds the user's id from the username, gets the information about the user from the * followers and critts table and outputs it into the others.profile view */ public function action_show($username) { // we get the user's id that matches the username $user_id = User::where('username', '=', $username)->only('id'); // declare some default values for variables $following = null; $followers = 0; // if the username is not found, display an error if ($user_id == null) { echo "This username does not exist."; } else { if (Auth::user()) { // if the user tries to go to his/her own profile, redirect to user's profile action. if ($user_id == Auth::user()->id) { return Redirect::to_action('user@index'); } // check if the current user is already following $username $following = Follower::where('user_id', '=', Auth::user()->id)->where('following_id', '=', $user_id)->get() ? true : false; } // eager load the critts with user data $allcritts = Critt::with('user')->where('user_id', '=', $user_id); // order the critts and split them in chunks of 10 per page $critts = $allcritts->order_by('created_at', 'desc')->paginate(10); // count the critts $critts_count = $allcritts->count(); // count the followers $followers = Follower::where('following_id', '=', $user_id)->count(); // bind data to the view return View::make('others.profile')->with('username', $username)->with('user_id', $user_id)->with('following', $following)->with('followers', $followers)->with('count', $critts_count)->with('critts', $critts); } }
/** * post_index validates input for the new critt and creates the critt in the DB */ public function post_index() { // get filtered input for the critt $new_critt = array('critt' => htmlspecialchars(Input::get('new_critt'))); // assign validation rules $rules = array('critt' => 'required|min:3|max:321'); // Make the validator $validation = Validator::make($new_critt, $rules); if ($validation->fails()) { return Redirect::to_action('user@index')->with('user', Auth::User())->with_errors($validation)->with_input(); } // attach current user's ID to the user_id in critts table $new_critt['user_id'] = Auth::user()->id; // create the new critt and get back to the user's profile $critt = new Critt($new_critt); $critt->save(); return Redirect::to_action('user@index'); }
/** * get_index checks if the user is logged in already, if not, it shows a default home page with options to * login or sign up, if logged in it will show a critter feed from all other users in the system */ public function get_index() { if (Auth::guest()) { return View::make('home.index'); } else { $critts = Critt::with('user')->order_by('created_at', 'desc')->paginate(10); $count = Critt::count(); return View::make('home.critterfeed')->with('count', $count)->with('critts', $critts); } }