Trying to get property ‘image’ of non-object in Laravel

  laravel

I am working on a laravel project. In this project, when i try to view and edit the profile page. I am getting this error.

ErrorException
Trying to get property ‘image’ of non-object (View: C:xampphtdocsHomeServicesresourcesviewslivewiresprovidersprovider-profile-component.blade.php)
http://127.0.0.1:8000/sprovider/profile

SproviderProfileComponent.php :-

<?php

namespace AppHttpLivewireSprovider;

use AppModelsServiceProvider;
use IlluminateSupportFacadesAuth;
use LivewireComponent;

class SproviderProfileComponent extends Component
{
    public function render()
    {
        $sprovider = ServiceProvider::where('user_id',Auth::user()->id)->first();
        return view('livewire.sprovider.sprovider-profile-component',['sprovider'=>$sprovider])->layout('layouts.base');
    }
}

Models/ServiceProvider.php :-

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;

class ServiceProvider extends Model
{
    use HasFactory;

    protected $fillable = ['user_id'];

    public function category()
    {
        return $this->belongsTo(ServiceCategory::class,'service_category_id');
    }
}

sprovider-profile-component.blade.php

<div class="panel-body">
    <div class="row">
        <div class="col-md-4">
            @if($sprovider->image)
                <img src="{{asset('images/sproviders')}}/{{$sprovider->image}}" width="100%" />
            @else
                <img src="{{asset('images/sproviders/default.jpg')}}" width="100%" />
            @endif
        </div>
            <div class="col-md-8">
                <h3>Name: {{Auth::user()->name}}</h3>
                <p>{{$sprovider->about}}</p>
                <p><b>Email: </b>{{Auth::user()->email}}</p>
                <p><b>Phone: </b>{{Auth::user()->phone}}</p>
                <p><b>City: </b>{{$sprovider->city}}</p>
                <p><b>Service Category: </b>
                @if($sprovider->service_category_id)
                    {{$sprovider->category->name}}
                @endif
                </p>
                <p><b>Service Locations: {{$sprovider->service_locations}}</b></p>
                <a href="{{route('sprovider.edit_profile')}}" class="btn btn-info pull-right">Edit Profile</a>
            </div>
    </div>
</div>

Source: Laravel

Leave a Reply