/** * Saves specified hashtag $term to database and associates the hashtag with * the user logged in if applicable. * * @param string $term hashtag term to save off */ protected function saveHashtag($term) { // store most recently searched hashtag term in the global session \Session::put('searched_term', $term); // if a user is logged, then associate the user with the hashtag // otherwise, store hashtag term in global session until user logs in if (\Auth::check()) { // get the user id logged in $user_id = \Auth::id(); // create a hashtag associated with the user if it does not exist \App\Hashtag::firstOrCreate(compact('term', 'user_id')); } else { if (!in_array($term, \Session::get('stored_terms', []))) { \Session::push('stored_terms', $term); } } }
/** * Saves the hashtag terms stored in the global session to the database. */ protected function saveStoredHashtags() { // get the user id logged in $user_id = \Auth::id(); // create a hashtag associated with the user if it does not exist // for each stored hashtag term in global session $stored_terms = \Session::pull('stored_terms', []); while (($term = array_pop($stored_terms)) != null) { \App\Hashtag::firstOrCreate(compact('term', 'user_id')); } }