Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [Laravel] Czy użytkownik jest online?
markuz
post
Post #1





Grupa: Zarejestrowani
Postów: 1 240
Pomógł: 278
Dołączył: 11.03.2008

Ostrzeżenie: (0%)
-----


Witam,

Na podstawie http://laravel.io/forum/03-03-2014-sentry-3-users-online
utworzyłem model Online oraz tabelę sessions.

Chciałbym dodać metodę która sprawdzi czy użytkownik jest zalogowany.

np.

  1. $user = User::find(1);
  2. if($user->isOnline())
  3. echo "Zalogowany";


Moje doświadczenie w Laravel jest bardzo niskie. Macie może jakieś porady jak się za to zabrać?

# models/Online.php

  1. <?php
  2.  
  3. use Illuminate\Database\Eloquent\Model;
  4.  
  5. //use Session;
  6.  
  7. class Online extends Model {
  8.  
  9. /**
  10.   * {@inheritDoc}
  11.   */
  12. public $table = 'sessions';
  13.  
  14. /**
  15.   * {@inheritDoc}
  16.   */
  17. public $timestamps = false;
  18.  
  19. /**
  20.   * Returns all the guest users.
  21.   *
  22.   * @param \Illuminate\Database\Eloquent\Builder $query
  23.   * @return \Illuminate\Database\Eloquent\Builder
  24.   */
  25. public function scopeGuests($query)
  26. {
  27. return $query->whereNull('user_id');
  28. }
  29.  
  30. /**
  31.   * Returns all the registered users.
  32.   *
  33.   * @param \Illuminate\Database\Eloquent\Builder $query
  34.   * @return \Illuminate\Database\Eloquent\Builder
  35.   */
  36. public function scopeRegistered($query)
  37. {
  38. return $query->whereNotNull('user_id')->with('user');
  39. }
  40.  
  41. /**
  42.   * Updates the session of the current user.
  43.   *
  44.   * @param \Illuminate\Database\Eloquent\Builder $query
  45.   * @return \Illuminate\Database\Eloquent\Builder
  46.   */
  47. public function scopeUpdateCurrent($query)
  48. {
  49.  
  50. return $query->where('id', Session::getId())->update(array(
  51. 'user_id' => Auth::user() ? Auth::user()->id : null
  52. ));
  53. }
  54.  
  55.  
  56. /**
  57.   * Returns the user that belongs to this entry.
  58.   *
  59.   * @return \Cartalyst\Sentry\Users\EloquentUser
  60.   */
  61. public function user()
  62. {
  63. return $this->belongsTo('User'); # Sentry 3
  64. // return $this->belongsTo('Cartalyst\Sentry\Users\Eloquent\User'); # Sentry 2
  65. }
  66.  
  67. }


#models/User

  1. <?php
  2.  
  3. use Illuminate\Auth\UserTrait;
  4. use Illuminate\Auth\UserInterface;
  5. use Illuminate\Auth\Reminders\RemindableTrait;
  6. use Illuminate\Auth\Reminders\RemindableInterface;
  7.  
  8. class User extends Eloquent implements UserInterface, RemindableInterface {
  9.  
  10. use UserTrait, RemindableTrait;
  11.  
  12. /**
  13. * The database table used by the model.
  14. *
  15. * @var string
  16. */
  17. protected $table = 'users';
  18.  
  19. /**
  20. * The attributes excluded from the model's JSON form.
  21. *
  22. * @var array
  23. */
  24. protected $hidden = array('password');
  25.  
  26. }



Update

Do modelu User dodałem metode isOnline:

  1. public function isOnline()
  2. {
  3. return Online::registered()->where('user_id', $this->getAuthIdentifier())->count();
  4. }


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 smile.gif




Ten post edytował markuz 21.12.2014, 17:08:59


--------------------
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 20.08.2025 - 18:47