skip to Main Content

I have a project with laravel 10.45 and livewire 3.

I try to edit an Company but i received this error "Attempt to assign property "id" on null"

My method Update.php have a mount method, like bellow

/** * @throws AuthorizationException */ public function mount(): void { $this->authorize(Can::BE_AN_ADMIN->value); $company = Company::findOrFail(request()->route('id')); $this->setCompany($company); }

I put some dd() in codes and I can saw that the variable $company return the value expected.

I put some dd into setCompany and I can saw that the variable $company return values, normaly.

So, the error said that i’m havent the property "id", like my $company variable is null.

I trying to solve this in diffents ways like put on IF before initialize the method setCompany, the problem is that this if is true
because i have the return inside variable $company.

Please, anyone here had the same problem?

[The dd into function setCompany] https://phpout.com/wp-content/uploads/2024/02/oTGmo.png

[Return in browser] https://phpout.com/wp-content/uploads/2024/02/JC2x5.png

I trying to solve this in diffents ways like put on IF before initialize the method setCompany, the problem is that this if is true
because i have the return inside variable $company.

I expected that the setCompany works normally

2

Answers


  1. Chosen as BEST ANSWER

    Thanks Karl Hill,

    The same error occurs.

    Note: the application is running with Docker and is running normally on another machine with not errors


  2. The setCompany method is likely trying to set the id property on this null Company object, hence the error. To fix this, you should check if the Company object is null before calling the setCompany method. H

    /** 
     * @throws AuthorizationException 
     */
    public function mount(): void 
    {
        $this->authorize(Can::BE_AN_ADMIN->value);
        
        $company = Company::find(request()->route('id'));
        
        if ($company) {
            $this->setCompany($company);
        } else {
            // Handle the case when the company is not found
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search