Something went wrong. Try again.
My earliest code I have saved
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379<?php
namespace ConnectU\Models;
use DB;use Auth;use Carbon\Carbon;use ConnectU\Models\Status;use Illuminate\Auth\Authenticatable;use Illuminate\Database\Eloquent\Model;use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
class User extends Model implements AuthenticatableContract{ use Authenticatable;
/** * The database table used by the model. * * @var string */ protected $table = 'users';
/** * The dates in the project * * @var array */ protected $dates = ['last_activity'];
/** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'email', 'username', 'first_name', 'last_name', 'password', 'location', 'biography', 'sex', 'verified', 'position', 'last_activity', 'ip', ];
/** * The attributes excluded from the model's JSON form. * * @var array */ protected $hidden = [ 'password', 'remember_token', 'ip', ];
/** * Get the users name. * * Firsts checks if both the users first and last name is defined * Then checks if just the first name is defined * * @return string */ public function getName() { if ($this->first_name && $this->last_name) { return "{$this->first_name} {$this->last_name}"; }
if ($this->first_name) { return $this->first_name; }
return null; }
/** * Gets the users full name or username * * Uses the getName() function. If nothing is returned from that * Get the users username * * @return string */ public function getNameOrUsername() { return $this->getName() ?: $this->username; }
/** * Gets the users first name or username * * @return string */ public function getFirstNameOrUsername() { return $this->first_name ?: $this->username; } /** * Grabs the users gravatar image * * Uses the md5 hash of the users email and uses the size specified * To generate the profile image * * @param int $size * @return string */ public function getAvatarUrl($size = 45) { return "http://www.gravatar.com/avatar/" . md5($this->email) . "?d=mm&s=" . $size; }
/** * Gets the users statuses * * Uses a hasMany relationship to see the users statuses by the user_id * * @return void */ public function statuses() { return $this->hasMany('ConnectU\Models\Status', 'user_id'); }
/** * Gets the users likes * * Uses a hasMany relationship to see the users likes by the user_id * * @return void */ public function likes() { return $this->hasMany('ConnectU\Models\Like', 'user_id'); }
/** * Gets the friends of the user * * Uses a belongsToMany relationship to determine what users are friends with the user * * @return void */ public function friendsOfMine() { return $this->belongsToMany('ConnectU\Models\User', 'friends', 'user_id', 'friend_id'); }
/** * Gets the friends of other users. Reversed of friendsOfMine() * * Uses a belongsToMany relationship to determine what users are friends with a user * * @return void */ public function friendsOf() { return $this->belongsToMany('ConnectU\Models\User', 'friends', 'friend_id', 'user_id'); }
/** * Gets the friendship between two users * * Uses the friendsOfMine() pivot where 'accepted' is true and gets the results. * Also, this merges the friendsOf() function too so you get both friendships you * have initiated and a different user has initiated. * * @return array */ public function friends() { return $this ->friendsOfMine() ->wherePivot('accepted', true) ->get() ->merge($this->friendsOf() ->where('accepted', true) ->get()); }
/** * Grabs the users friend requests * * Uses the friendsOfMine() to determine if we have any friend requests pending. * We use friendsOfMine() because that is logic for when a DIFFERENT user(not the logged in user) * Initiates a friendship instance * * @return array */ public function friendRequests() { return $this->friendsOfMine()->wherePivot('accepted', false)->get(); }
/** * Grabs the users pending friend requests * * Uses the friendsOf() to determine the friendships that we have for other users * that are still pending, not accepted. We use friendsOf() because it shows * friendship instances where we initiated it. * * @return array */ public function friendRequestsPending() { return $this->friendsOf()->wherePivot('accepted', false)->get(); } /** * Checks to see if a specified user has any friend requests pending * * Using the count of friendRequestsPending(), where the users id is present, if it * is 1, then it returns true, else returns false * * @param $user - User model * @return boolean */ public function hasFriendRequestPending(User $user) { return (bool) $this->friendRequestsPending()->where('id', $user->id)->count(); }
/** * Checks to see if a specified user has any friend requests recieved * * Using the friendRequests(), where the users id is present * * @param $user - User model * @return boolean */ public function hasFriendRequestReceived(User $user) { return (bool) $this->friendRequests()->where('id', $user->id)->count(); }
/** * Add a friendship in the database * * Makes a new friendsOf() instance beween the logged in user and * specified user * * @param $user - User model * @return none */ public function addFriend(User $user) { $this->friendsOf()->attach($user->id); }
/** * Deletes a friendship in the database * * Does a few checks to make sure that there is a user present and that you * Two are friends, and then detaches the friendship. * * @param $user - User model * @return none */ public function removeFriend(User $user) { if (!Auth::user()->isFriendsWith($user)) { return redirect()->back()->with('dang', 'You are not friends with' . $user->getFirstNameOrUsername()); }
if (!$user) { return redirect()->back()->with('dang', 'That is not a real user!'); }
$this->friendsOf()->detach($user->id); $this->friendsOfMine()->detach($user->id); }
/** * Accepts a friend request * * Using the friendRequests(), where the users id is selected, take the pivot and * Update the 'accepted' row to a 1 or 'true' * * @param $user - User model * @return none */ public function acceptFriendRequest(User $user) { $this->friendRequests()->where('id', $user->id)->first()->pivot->update([ 'accepted' => true, ]); }
/** * Checks to see if the current user is friends with a specified user * * Using the friends() function, it counts the records of frienships between * the currently logged in user to a specified user, it then converts that to * a boolean. * * @param $user - User model * @return boolean */ public function isFriendsWith(User $user) { return (bool) $this->friends()->where('id', $user->id)->count(); }
/** * Checks to see if a specified user has any friend requests recieved * * Using the friendRequests(), where the users id is present * * @param $status - Status Model * @return boolean */ public function hasLikedStatus(Status $status) { return (bool) $status->likes()->where('user_id', $this->id)->count(); }
/** * A simple way to check if the user is an administrator * * USAGE: * if ($user->admin) * It will return true if the user is an administrator * * @return boolean */ public function getAdminAttribute() { return $this->position === 'admin'; }
/** * Check if the user has a ceratin position * * It checks if the passed position is equal to the users position row * In the database. * * Example use: * if ($user->hasPosition('mod')) * It will return true if the user is a moderator * * @param $position * @return boolean */ public function hasPosition($position = null) { $position = is_array($position) ? $position : func_get_args();
return in_array($this->position, $position); }
/** * Reloads the users 'last_activity' row * * Updates the users 'last_activity' row to the current time, minus the * 5 hours is optional, because the hosting server is 5 hours ahead. * * @return none */ public function reloadActivityTime() { $current_time = Carbon::now();
$this->update([ 'last_activity' => $current_time, ]); }
public function isVerified() { return (bool) $this->verified; }}