Laravel Livewire error "must not be accessed before initialization"

Laravel Livewire error "must not be accessed before initialization"

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Resolving the "Typed Property Must Not Be Accessed Before Initialization" Error in Livewire

You are encountering a specific PHP error because you are defining a property as a typed instance of a model (public Country $country;) but failing to initialize it before attempting to access its methods. The error message Typed property App\Http\Livewire\V2\Settings\Locations::$country must not be accessed before initialization is a hard stop from the PHP engine. It occurs because the object exists, but the variable holding it is null. In your SaveCountry method, you attempt to call $this->country->save() immediately after validation. If the property was never assigned an instance of the Country class in the constructor or prior logic, PHP throws this fatal error before the save operation can even begin.

The fundamental issue lies in how state is managed within Livewire components. Unlike standard controllers where you might instantiate a model explicitly, Livewire properties are often expected to be populated by data returned from the component's lifecycle or user input. In your current implementation, $this->country is declared as a typed property without an initial value. When SaveCountry executes and calls validation, the property remains uninitialized. The subsequent line attempts to access it as if it were already an object instance.

To fix this, you must ensure the property holds a valid instance of the Country model before you call its methods. There are two primary patterns for handling this in Livewire, depending on your specific use case.

Explicit Initialization via Constructor

The most robust approach is to define the default state of the component in the constructor. This ensures that whenever a new Locations instance is created, the $country property is guaranteed to be an object rather than null. You can pass a fresh model instance or a placeholder.

class Locations extends Component
{
    public Country $country;

    use WithPagination;

    protected $rules = [
        'country.name' => 'required|min:100',
        'country.note' => 'required|min:100',
    ];

    public function __construct()
    {
        // Initialize with a fresh model instance to satisfy the type hint
        $this->country = new Country();
    }

    public function SaveCountry()
    {
        $this->validate();
        
        // Now safe to access properties and methods
        $this->country->name = request('country.name');
        $this->country->note = request('country.note');
        
        $this->country->save();
        $this->reset();
        
        session()->flash('info', 'The country has been added successfully.');
    }

    public function render()
    {
        return view('livewire.v2.settings.locations', [
            'countries' => Country::orderBy('order_num', 'desc')->paginate(10),
        ]);
    }
}

By instantiating new Country() in the constructor, you remove the risk of accessing a null property. This pattern is particularly useful when the form submission needs to create a new record rather than updating an existing one.

Handling Existing Records

If your intention was to load an existing country for editing, you should populate $this->country inside the update() method or within the component's initialization logic based on request parameters. However, if you are strictly creating new records, the constructor approach is cleaner and avoids the "accessed before initialization" error entirely.

Data Binding Considerations

Your source material mentions using Livewire binding directly to model properties. While powerful, this requires careful handling of the property lifecycle. When binding a form input to $this->country, ensure that the model instance exists in memory. If you rely on Livewire's magic methods or event listeners to populate this property, verify that those events fire before any action attempts to read the object.

In many cases, developers mistakenly assume that validation sets the property value automatically. It does not; validation only checks the input data against rules defined in $rules. You must manually map validated request data to the model properties or assign the whole request payload to the model instance after validation passes but before calling save().

Best Practices for State Management

When working with typed properties in Livewire, always ask: "What is the default state?" If a property can be null and you plan to access it later, initialize it. Alternatively, use loose typing (public ?Country $country;) if null is a valid state, though this sacrifices some of the safety provided by strict types. For creating new resources, explicit initialization in the constructor is the standard pattern that prevents runtime errors and makes the component's state predictable.

For further guidance on managing Livewire component state and data binding patterns, you might want to review the documentation at https://laravelcompany.com, which often covers advanced usage scenarios for these specific edge cases.

Our network