Home > Back-end >  Trying to get property 'image' of non-object in Laravel
Trying to get property 'image' of non-object in Laravel

Time:09-16

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:\xampp\htdocs\HomeServices\resources\views\livewire\sprovider\sprovider-profile-component.blade.php) http://127.0.0.1:8000/sprovider/profile

SproviderProfileComponent.php :-

<?php

namespace App\Http\Livewire\Sprovider;

use App\Models\ServiceProvider;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;

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 App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

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>

CodePudding user response:

The only reason this could happen is because $sprovider is null.

When it happens, $sprovider->image will translate to (null)->image, which is indeed Trying to get property 'image' of non-object.

You could do this to prevent $sprovider from being null:

$sprovider = ServiceProvider::where('user_id',Auth::user()->id)->firstOrFail(); 

By using firstOrFail instead of first, you ensure $sprovider will never be null (like the name suggests, if it doesn't find any provider, it will fail).

You will have another error saying that no provider could be found, this is another issue, probably because you don't have any provider for this user or something like that.

CodePudding user response:

Two possibilities

  1. $sprovider is null and

  2. image key is not available in $sprovider ($sprovider->image) may be you mis-spelled image in database table so for that just use dd($sprovider); or print_r($sprovider); and check image key is available or not.

  • Related