Collin
Collin

Reputation: 944

Laravel - Trying to get property 'name' of non-object

Im making a view that shows the users and the companies they work for.

In my User model i return the view like this:

return view('users.index', ['users' => $model->all()]);

The model $model->all() is my User model.

Then in my index.blade file i loop through the data like this:

@foreach ($users as $user)
                                <tr>
                                    <td>{{ $user->name }}</td>
                                    <td>
                                        <a href="mailto:{{ $user->email }}">{{ $user->email }}</a>
                                    </td>
                                    <td>{{ $user->created_at->format('d/m/Y H:i') }}</td>
                                    <td>{{ $user->company->name }}</td>
                                    <td class="text-right">
                                        <div class="dropdown">
                                            <a class="btn btn-sm btn-icon-only text-light" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                                <i class="fas fa-ellipsis-v"></i>
                                            </a>
                                            <div class="dropdown-menu dropdown-menu-right dropdown-menu-arrow">
                                                @if ($user->id != auth()->id())
                                                    <form action="{{ route('user.destroy', $user) }}" method="post">
                                                        @csrf
                                                        @method('delete')

                                                        <a class="dropdown-item" href="{{ route('user.edit', $user) }}">{{ __('Edit') }}</a>
                                                        <button type="button" class="dropdown-item" onclick="confirm('{{ __("Are you sure you want to delete this user?") }}') ? this.parentElement.submit() : ''">
                                                            {{ __('Delete') }}
                                                        </button>
                                                    </form>    
                                                @else
                                                    <a class="dropdown-item" href="{{ route('profile.edit') }}">{{ __('Edit') }}</a>
                                                @endif
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                            @endforeach

But when i try to show $user->company->name i get the following error:

Trying to get property 'name' of non-object

I tried debugging by using dd($user->company) and it showed all the company information of the company connected to the user. Then i tried dd($user->company->name) and it showed the company name.

I dont understand why i cant just show $user-company->name without getting the error but when i dd($user->company->name) it shows the company name.

Can someone point me in the right direction.

Upvotes: 1

Views: 179

Answers (3)

Anthony Aslangul
Anthony Aslangul

Reputation: 3847

It probably shows an error because one of your users doesn't have a company. The first one of your loop has one, that's why when you dd($user->company), you see a company. But it doesn't mean the second or third has one. You can do a if statement in order to catch any null company. Something like

if($user->company == null){ 
    dd($user); 
}

Alternatively, you can just do dd($users->pluck('company','id'));, which will show you a collection with the user id as key and related company (or null) as value.

Upvotes: 3

Bhavesh Darji
Bhavesh Darji

Reputation: 1

If you are trying to get relationship object value might be chances are there you will get this type of error. Due to there is some user who doesn't have company detail.

Just try below code.

{{ @$user->company->name }}

Upvotes: 0

Richie
Richie

Reputation: 1439

You can use optional() helper to solve this. There are users who don't have companies. SO you can display them like this optional($user->company)->name and you will not get an error if a user does not have a company

Upvotes: 1

Related Questions