Witam,
Na podstawie
http://laravel.io/forum/03-03-2014-sentry-3-users-onlineutworzyłem model Online oraz tabelę sessions.
Chciałbym dodać metodę która sprawdzi czy użytkownik jest zalogowany.
np.
$user = User::find(1);
if($user->isOnline())
Moje doświadczenie w Laravel jest bardzo niskie. Macie może jakieś porady jak się za to zabrać?
# models/Online.php
<?php
use Illuminate\Database\Eloquent\Model;
//use Session;
class Online extends Model {
/**
* {@inheritDoc}
*/
public $table = 'sessions';
/**
* {@inheritDoc}
*/
public $timestamps = false;
/**
* Returns all the guest users.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeGuests($query)
{
return $query->whereNull('user_id');
}
/**
* Returns all the registered users.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeRegistered($query)
{
return $query->whereNotNull('user_id')->with('user');
}
/**
* Updates the session of the current user.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeUpdateCurrent($query)
{
return $query->where('id', Session
::getId())->update(array( 'user_id' => Auth::user() ? Auth::user()->id : null
));
}
/**
* Returns the user that belongs to this entry.
*
* @return \Cartalyst\Sentry\Users\EloquentUser
*/
public function user()
{
return $this->belongsTo('User'); # Sentry 3
// return $this->belongsTo('Cartalyst\Sentry\Users\Eloquent\User'); # Sentry 2
}
}
#models/User
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected
$hidden = array('password');
}
UpdateDo modelu User dodałem metode isOnline:
public function isOnline()
{
return Online::registered()->where('user_id', $this->getAuthIdentifier())->count();
}
i działa tak jak chciałem - jednak nie wiem czy jest to najlepsze rozwiązanie - gdyby ktoś miał coś lepszego jestem otwarty na propozycje
Ten post edytował markuz 21.12.2014, 17:08:59