Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ComputerMaverick's avatar

Display Model Relationship Data in Blade view

So i have a User model with multiple relationships with Organization Model, Profile Model.

In my controller i have this function

public function index()
    {
        $users = User::with(['Organization', 'Profile'])->where('id', 	Auth::user()->id)->get();
        //dd($users);
        return view('home')->with('users', $users);
    }

Now as per my knowledge, i should be able to access the relationship data in the blade template as below:

@if(count($users) > 0)
	@foreach($users as $user)
		<h1>{{ $user->organization->organization_name }}</h1>
		<h1>{{ $user->profile->first_name }}</h1>
		<h1>{{ $user->profile->last_name }}</h1>
	@endforeach
@else
	<h1>No User Found</h1>
@endif

However the above shows an error message of: Property [organization_name] does not exist on this collection instance.

What i noticed

  • 1. If the user has an organization data, i can access it using {{$user->organization[0]->organization_name}}
  • 2. If there is no data, it shows an error message.
  • 3. As per documentation one should be able to access the data without using the array index
0 likes
5 replies
ajithlal's avatar

Can you show us your user model relationships you wrote on your user model for Organization and Profile? A user hasOne organization and one Profile.

with

$users = User::with(['Organization', 'Profile'])->where('id', 	Auth::user()->id)->get();

this line of code I'm assuming that your relationship on your User model is Something like

public function Organization()
{
	$this->hasOne('App\Organization','user_id');
}
public function Profile()
{
	$this->hasOne('App\Profile','user_id');
}

So you have to access it something like:

$user->Organization->organization_name
ComputerMaverick's avatar

User Model

function profile(){
        return $this->hasOne('App\Profile');
    }

    function organization(){
        return $this->hasOne('App\Organization');
    }

    public function getFullNameAttribute(){
        return "{$this->profile->first_name} {$this->profile->last_name}";
    }

    public function getProfilePicAttribute(){
        return "{$this->profile->profile_pic}";
    }

    public function getUserLocationAttribute(){
        return "{$this->profile->state}, {$this->profile->country}";
    }

Profile Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Profile extends Model
{
    function user(){
        return $this->belongsTo('App\User');
    }
}

Organization Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Organization extends Model
{
    function user(){
        return $this->belongsTo('App\User');
    }
}
ajithlal's avatar

rewrite your code

public function index()
    {
        $users = User::with(['organization', 'profile'])->where('id', 	Auth::user()->id)->get();
        //dd($users);
        return view('home')->with('users', $users);
    }

in view

@if(count($users) > 0)
	@foreach($users as $user)
		<h1>{{ $user->organization->organization_name }}</h1>
		<h1>{{ $user->profile->first_name }}</h1>
		<h1>{{ $user->profile->last_name }}</h1>
	@endforeach
@else
	<h1>No User Found</h1>
@endif

hope this will work

Snapey's avatar

This code gets a collection of one user

public function index()
    {
        $users = User::with(['Organization', 'Profile'])->where('id', 	Auth::user()->id)->get();
        //dd($users);
        return view('home')->with('users', $users);
    }

since you use id and there will only be one record with that id.

I would change it as;

public function index()
    {
        $user = User::with(['Organization', 'Profile'])->findOrFail(Auth::id());

        return view('home')->with('user', $user);
    }

Then you don't need to count or iterate over collection.

		<h1>{{ $user->organization->organization_name ?? ''}}</h1>
		<h1>{{ $user->profile->first_name ?? '' }}</h1>
		<h1>{{ $user->profile->last_name ?? '' }}</h1>

Whenever you use two -> operators in model data you should get in the habit of using null coalesce operator ?? to guard against the related model being missing.

So, for instance, if $user has no organisation, then your original code would blow up

Snapey's avatar
Snapey
Best Answer
Level 122

Also, if this page can be accessed by guests then you have a problem because your code assumes that there will be an authenticated user. You could get around this in multiple ways such as having a different view for guests.

    public function index()
    {

	if(! Auth::check() {
            return view ('guest.home');
	}

        $user = User::with(['Organization', 'Profile'])->findOrFail(Auth::id());

        return view('home')->with('user', $user);
    }

Please or to participate in this conversation.