where $userAccount come from ?
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!
@georgetown74 eager load the relationship get the results first...
UserApp::with('userAccounts')->get()
Please or to participate in this conversation.