FounderStartup's avatar

ErrorException Attempt to read property "city_name" on null

Getting this error on production server though its working fine on local ?

ErrorException Attempt to read property "city_name" on null (View: /home/buzzinghouses/public_html/demo/resources/views/frontend/broker/searchbrokers.blade.php)

<li class="mr-5"><a href="#" class="icons text-muted"><i class="fa fa-map-marker text-muted mr-1"></i>{{ $item->cityname->city_name }}</a></li>

0 likes
11 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

That happens when there isn't any relationship found.

You can add a fallback

{{ $item->cityname->city_name ?? 'no city' }}

Or give some more context as to what $item is and how the relationship looks

1 like
FounderStartup's avatar

@Sinnbeck model

    public function cityname(){
    	return $this->belongsTo(Cities::class,'city');
    }

Controller:

    public function SearchBrokers(){

        $brokerscount = User::where('role', 2 )->where('status', 1)->count();
        $brokers = User::with('cityname')->where('role', 2 )->where('status', 1)->paginate(10);
        $cities = Cities::where('status', 1)->orderBy('city_name','ASC')->latest()->get();

  	    return view('frontend.broker.searchbrokers',compact('brokerscount','brokers', 'cities'));

    } // end method

View:

                                    <div class="tab-content">
                                        <div class="tab-pane active" id="tab-11">
                                            @foreach($brokers as $item)

                                            <div class="card overflow-hidden">
                                                <div class="card-body">
                                                    <div class="item-det row">
                                                        <div class="col-md-9">

                                                            <div class="d-flex footerimg-l mb-0">
                                                                <img src="{{ (!empty($item->profile_photo_path))? url('upload/user_images/'.$item->profile_photo_path):url('upload/no_profile_image.jpg') }}" class="avatar cover-image avatar-xl brround">
                                                            </div>

                                                                @php
                                                                    $recommendationscount = App\Models\RealtorReviews::where('realtor_id',$item->id)->where('status',1)->count();
                                                                    // $avarage = App\Models\RealtorReviews::where('realtor_id',$item->id)->where('status',1)->avg('rating');
                                                                @endphp

                                                                @if ( $recommendationscount == 1 )
                                                                    <a href="{{ route('business.profile',$item->id) }}" class="text-muted"><h4 class="mb-2"><b>{{ $item->name }}</b> | <i class="fa fa-smile mr-1 text-warning mr-1"></i>{{ $recommendationscount }} Recommendation</h4></a>
                                                                @elseif ($recommendationscount > 1)
                                                                    <a href="{{ route('business.profile',$item->id) }}" class="text-muted"><h4 class="mb-2"><b>{{ $item->name }}</b> | <i class="fa fa-smile mr-1 text-warning mr-1"></i>{{ $recommendationscount }} Recommendations</h4></a>
                                                                @else
                                                                    <a href="{{ route('business.profile',$item->id) }}" class="text-muted"><h4 class="mb-2"><b>{{ $item->name }}</b></h4></a>
                                                                @endif

                                                            <div class="">
                                                                <ul class="mb-0 d-flex">

                                                                    <li class="mr-5"><a href="#" class="icons text-muted"><i class="fa fa-map-marker text-muted mr-1"></i>{{ $item->cityname->city_name }}</a></li>
                                                                    @php
                                                                        $activelistings = App\Models\Listings::where('user_id',$item->id)->where('status',1)->count();
                                                                        $totalinprogress = App\Models\Listings::where('user_id',$item->id)->where('status',1)->where('dealstatus','inprogress')->count();
                                                                        $totalclosed = App\Models\Listings::where('user_id',$item->id)->where('status',1)->where('dealstatus','closed')->count();
                                                                    @endphp

                                                                    @if ($activelistings > 0)
                                                                        <li class="mr-5"><a href="#" class="text-muted"><i class="fa fa-list mr-1 text-muted mr-1"></i>Active listings : {{ $activelistings }}</a></li>
                                                                    @endif
                                                                    @if($totalinprogress > 0)
                                                                        <li class="mr-5"><a href="#" class="text-muted"><i class="fa fa-list mr-1 text-muted mr-1"></i> Listings in progress : {{ $totalinprogress }} </a></li>
                                                                    @endif
                                                                    @if($totalclosed > 0)
                                                                        <li class="mr-5"><a href="#" class="text-muted"><i class="fa fa-list mr-1 text-muted mr-1"></i> Deals Closed : {{ $totalclosed }}</a></li>
                                                                    @endif
                                                                </ul>
                                                            </div>
                                                        </div>
                                                        <div class="col-sm-3 col-auto">

                                                            <div class="icons mt-3 mt-sm-0 pb-0 ">
                                                                <a href="{{ route('business.profile',$item->id) }}" class="btn btn-info mt-1">Check Profile</a>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            @endforeach
                                            {{ $brokers->links() }}
                                        </div>
                                    </div>
Sinnbeck's avatar

@FounderStartup ok so it's brokers.

Try this

{{ $item->cityname->city_name ?? $item->city_id }}

This should give you the city_id it cannot find. See if that actually exists in the cities table (my guess is that it does not)

FounderStartup's avatar

@Sinnbeck

model :

    public function cityname(){
    	return $this->belongsTo(Cities::class,'city');
    }

Controller :

 public function SearchBrokers(){

        $brokerscount = User::where('role', 2 )->where('status', 1)->count();
        $brokers = User::with('cityname')->where('role', 2 )->where('status', 1)->paginate(10);
        $cities = Cities::where('status', 1)->orderBy('city_name','ASC')->latest()->get();

  	    return view('frontend.broker.searchbrokers',compact('brokerscount','brokers', 'cities'));

    } // end method

My query is that its working on local but not on production server ? Though I have added your code but still wondering why it happened ?

Sinnbeck's avatar

@FounderStartup yes I know. Did you try debugging like I suggested? One of the rows in brokers has a city_id that does not exist in the cities table

Do this and the one with the error will have a number instead of a city

{{ $item->cityname->city_name ?? $item->city_id }}
1 like
Sinnbeck's avatar

@FounderStartup great. Another solution is to only load brokers with cities

$brokers = User::with('cityname')
->has('cityname') //this
->where('role', 2 )->where('status', 1)->paginate(10); 
1 like

Please or to participate in this conversation.