#Attempt to read property service_status_id on null

63 messages · Page 1 of 1 (latest)

mint light
royal trout
#

Show the whole error, it sounds like you're probably trying to read a value on a relationship without loading said relationship.

mint light
#

But when I delete the last instance it works very well

#

The controller can read the relationship

mint light
royal trout
#

The error isn't saying service_status_id is null, it is saying whatever you're trying to check that variable on is null.

#

Show the whole error.

mint light
#

oooooh ok

#

[2022-10-13 20:21:54] local.ERROR: Attempt to read property "service_status_id" on null (View: /home/bujiybc/hopassist/resources/views/admin/Service/dashboard.blade.php) {"view":{"view":"/Service/dashboard.blade.php","data":{"errors":"<pre class=sf-dump id=sf-dump-2021905783 data-indent-pad=" "><span class=sf-dump-note>Illuminate\Support\ViewErrorBag</span> {<a class=sf-dump-ref>#1759</a><samp data-depth=1 class=sf-dump-expanded>
#<span class=sf-dump-protected title="Protected property">bags</span>: []
</samp>}

royal trout
mint light
#

But why is the relationship is load before ?

royal trout
#

I don't understand your question, sorry.

mint light
#

I already have data in the database Service and ServiceTracking, and the relation is load in the view

#

but now when I create a new Service I have this problem

#

I already create 4 Services and never have this problem

royal trout
#

Did those services all have ServiceTracking relationships? Maybe the problem is that the service you're adding is missing a serviceTracking relationship and you're not checking if the relationship exists before trying to access a field on it.

mint light
#

Yes they all have a ServiceTracking

royal trout
#

Adjust your code to instead be

  @foreach ($services as $service_wait)
    @if ($service_wait->serviceTracking && $service_wait->serviceTracking->service_status_id == 1)
mint light
#

But they have a ServiceTracking

#

it's required when create a Servic

#

ServiceTracking Model have this

royal trout
#

That isn't serviceTracking .. that is serviceStatus

mint light
#

In the ServiceTracking Model I load service status

royal trout
#

The problem isn't your serviceTracking -> serviceStatus relationship. It is service -> serviceTracking relationship either not existing or not being loaded.

mint light
#

oh okay

#

public function serviceTracking()

{

    return $this->hasOne(ServiceTracking::class);

}
#

I have this

royal trout
#

That defines the relationship, but that doesn't mean it is loaded when you grab the service.

#

Take a look at the eagerloading documentation I posted.

mint light
#

It is load because old Service work well

#

I can see it at the view so the relation is load

#

it's just now when I create a new one

royal trout
#

Are you sure you're creating the relationship when you create a new one?

mint light
#

That's why I don't understand the problem

#

Yes

royal trout
#

Show

mint light
#

All Services have ServiceTracking

#

So when I delete the last one all works very well

#

I delete the last Service, the last ServiceTracking etc ..

royal trout
#

Show the services too.

mint light
#

And I didn't touch the code before it

#

it's like ths

royal trout
#

Show your new one that is breaking it.

mint light
royal trout
#

Can you show your controller code?

mint light
#

yes

mint light
royal trout
#
 @if(!$service_wait->serviceTracking) 
    dd($service_wait)
  @endif
mint light
#

Thanks you very much for the help ! very very much I'm very in panic

royal trout
#

Add that inside your foreach, show me which service is failing.

mint light
#

okay

#

Undefined property: Illuminate\Pagination\Paginator::$serviceTracking

#

I have this error

royal trout
#

Eh? Show me the code with my update

mint light
#

yes I do it

royal trout
#

Actually might be better to just dump the contents of $services in your controller and show me the output.

mint light
#

Okay !

mint light