georgetown74's avatar

Returning UserAccount info with UserApp info, check if values are null

I have a two-part question. I am building a list of users that needs to pull information from a user and user_account table (with models of UserApp and UserAccount respectively). And then based on information from the UserAccount table I need to display the type of authentication method they are using (details below).

I set up a one-to-one relationship on the UserApp model and a belongs to relationship on the UserAccount model.

On my UserAppController I currently have the following to pull back information from the user table (I will be adding pagination later):

    public function index()
    {
        return view('user.userApp', [
            'userApps' => UserApp::all(),
        ]);
    }

Question 1: In order to also pull in the associated UserAccount information do I need to add a with element or something?

Next, in my table listing of users I want to show how they are authenticating into the app, which is based on whether they have a value for the user_account.password_hash, google_id, or facebook_id columns (that is, if they have a non-null value for password_hash then they are using the app's authentication, but if they have a non-null value for google_id then they are authenticating with Google, etc.). Here is the code I have in my userApp.blade.php file:

<td class="whitespace-nowrap px-11 py-4 text-sm text-gray-500">
     {{--TODO: this code is not working yet--}}
     @if(isset($userAccount->google_id))
            <img src="{{ asset('img/google.svg') }}" width = "25" height = "25" alt="Google Icon">
     @elseif(isset($userAccount->facebook_id))
             <img src="{{ asset('img/facebook.svg') }}" width = "25" height = "25" alt="Facebook Icon">
     @else
             <img src="{{ asset('img/app_icon.svg') }}" width = "25" height = "25" alt="App Icon">
     @endif
</td>

Question 2: Should the isset work in the code above once I resolve Question 1 above, or would I need to do something different, such as @if(isset($userApp->userAccount->google_id))

Thanks in advance!

0 likes
7 replies
georgetown74's avatar

@newbie360 $userAccount is probably not correct. I have a model called UserAccount with the following relationship to the UserApp model:

    public function userApp()
    {
        return $this->belongsTo(UserApp::class);
    }
georgetown74's avatar

@newbie360 And the UserApp model has this relationship to the UserAccount model:

    public function userAccount()
    {
        return $this->hasOne(UserAccount::class);
    }
newbie360's avatar
Level 24

@georgetown74 eager load the relationship get the results first...

UserApp::with('userAccounts')->get()
georgetown74's avatar

@newbie360 Thank you! Can you please clarify where I specify the eager loading? For example, I tried it in the UserAppController.php changing it from this...

    public function index()
    {
        return view('user.userApp', [
            'userApps' => UserApp::all(),
        ]);
    }

... to this:

    public function index()
    {
        return view('user.userApp', [
            'userApps' => UserApp::with('userAccounts')->get(),
        ]);
    }

... but when I did this I got the error message "Call to undefined relationship [userAccounts] on model [App\Models\UserApp]."

georgetown74's avatar

@newbie360 Thank you again. It's actually a one-to-one relationship so it should be singular. But after eager loading on the UserAppController I also had to specify the name of the foreign key in my model. So the final solution was as follows:

  1. Update UserAppController to eager load the userAccount data:
    public function index()
    {
        return view('user.userApp', [
            'userApps' => UserApp::with('userAccount')->get(),
        ]);
    }
  1. Specify the foreign key in the UserApp model:
    public function userAccount()
    {
        return $this->hasOne(UserAccount::class, 'user_id');
    }
  1. Specify the foreign key in the UserAccount model:
    public function userApp()
    {
        return $this->belongsTo(UserApp::class, 'user_id');
    }

And then once these changes were made, the isset() worked perfectly (which was part 2 of my question).

Thanks so much @newbie360 !

Please or to participate in this conversation.