4

I'm trying to find out how to return the relations I set up for my User model when using Sentry 2.

Normally, I have a user retrieved like this:

$user = User::find(1);

// get some relation
return $user->profile->profile_name;

However, now when I have Sentry 2 implemented I can retrieve the logged in user like this:

$user = Sentry::getUser();

This way I can easily access the users table in my DB, but how do I go about fetching the relations and methods I have set up in my User.php model?

This seems way to clumpsy, and not Laravel-ish:

User::find(Sentry::getUser()->id)->profile->wizard_completed;

It seems somewhat... backwards.

Can you guys help me out? =)

Marty
  • 463
  • 2
  • 6
  • 14

1 Answers1

21

Extend Sentry creating your own User model with your relations on it:

<?php

use Cartalyst\Sentry\Users\Eloquent\User as SentryModel;

class User extends SentryModel {

    public function profile()
    {
        return hasOne(...);
    }

}

Publish sentry's config:

php artisan config:publish cartalyst/sentry

And tell Sentry to use your model, which now extending Sentry's has all of its functionalities plus yours:

'model' => 'User',

Then you'll be able to:

echo Sentry::getUser()->profile->wizard_completed;
Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
  • Thank you, I will try that! But if I tell Sentry to use my model instead, won't some functionality of their model get lost? – Marty Feb 19 '14 at 21:17
  • 2
    If you are extending (´class User extends SentryModel´) their own model, how could it loose anything? It will have all Sentry's functionality plus yours. – Antonio Carlos Ribeiro Feb 19 '14 at 21:34
  • Than you, It makes perfekt sense that it would extend all its functionality. I just got confused when you said I should change the Sentry config to use my model. But since that model now extends theirs, I get it :) Thank you for your help! – Marty Feb 20 '14 at 06:44