Instance of \App\[MyModel]

Hello

A question:
How does livewire make an instance of my model with direct model binding?
When i create a public variable of my model and want to use it in an input field (wire:model=“mymodel.field”) there is an error that the there is no instance of my model.

How do i need to set this up?
And where are those instance stored? In the session?

When i use just a string, there is no issue.

Tobi

If you want to pass a variable into your input you can do the following

<input type="text" wire:model="name"/>
...
public $yourModel;

public function mount()
{
   $this->yourModel = $yourModel->first();
    $this->name = $this->yourModel->name;
}
...

in case you want to update your form, you can do the following

routes.php

Route::get('update-user/{user}', UpdateUser::class)->name('user.update');

update.blade.php

<input type="text" wire:model="name"/>

UpdateUser component

...
public $name;
public function mount(User $user)
{
 $this->name = $user->name;
}

Thank you for the answer.

How is it when i want to add a new record?
Maybe something is not working as supposed in my project?

When i declare

public Mymodel $mymodel

and add

wire:model=”‘mymodel.name”

to an input field

(and add the rule)

i get the error:

Fatal error: Uncaught Error: Typed property xxx
must not be accessed before initialization

do i do something wrong?

i do it like described here

Thanks

Give me an example of your code is better.

My code isn’t really that different:

Model:
\App\Bill

Livewire Component

CreateInvoice.php

namespace App\Http\Livewire;

use App\Rules\AlphaLatinUmlaut;
use App\Rules\Iban;
use Livewire\Component;
use App\Bill;


class CreateInvoice extends Component
{
public Bill $bill;

public function rules()
{
    //validation rules
}

public function mount()
{
   $this->bill = new Bill();  // i tried both variants. with this and without this.
}

public function updated($propertyName)
{
    $this->validateOnly($propertyName);
}

create-invoice.blade.php

<form wire:submit.prevent="save">

<input wire:model="bill.Name" type="text" class="....">

</form

As soon as i want to enter something into the field i get the error.

Typed property App\Http\Livewire\CreateInvoice::$bill must not be accessed before initialization


The point is: Its the form field to add a record to the database. Its not yet persisted.
How does laravel keep track of the component properties while its on screen.

public $name = "John";

Where is the value of $name stored when it changes between (xhr) requests. In the session?

Tobi

First thing rules is a property and it’s not a method

    protected $rules = [
        'post.title' => 'required|string|min:6',
        'post.content' => 'required|string|max:500',
    ];

and wire:model is a two-way data binding you can not access a value in the bill object in wire:model directly you must do the following

...
public $bill;
public $name;
public function mount(Bill $bill)
{
  // in case you are reteriving a singal record (show or edit method for example)
  $this->bill = $bill;
  $this->name = $bill->name;
}
...
<input type="text" wire:model="name"/>

Read More here

Or here

Thanks for the fast reply

Im a little confused

xlaravel-livewire.com/docs/2.x/properties#binding-models
(remove x. this forum deletes the anchor)

states my example

If an Eloquent model is stored as a public property on a Livewire component, you can bind to its properties directly. Here is an example component

Rules method

And the rules is in the docs a property but when you look in the code of livewire its both… But maybe unsupported. I did use in as a method to add my custom validation rules. Like i do it in a Request class.

 => ['required',new AlphaLatinUmlaut()],

Understood, but what’s the issue with your code exactly?

That there is no instance of my Model?

Typed property App\Http\Livewire\CreateInvoice::$bill must not be accessed before initialization

In the livewire doc they dont the make no new instance of their model post and somehow it should work?

Tobi

Use dependency injection in the mount method

...
public function mount(Bill $bill)
{
   // your logic
}
...

But the example has no mount method at all?

I think this is not working as supposed?

There is only a typed public property and the rules constant

and then the direct access in the blade view

Livewire will take care of hydrating and dehydrating the model between requests with the current, non-persisted data.

I think this is not working. A bug or some kind of problem in my project.

i will try to make the example in a new project now.

Your example is a different concept? I was thinking to make it this way as well.

Thank you for your help!

Alright,

Feel free to ask or DM me if you have any issue/problem with your code in the future.

Happy coding!

Yeah its not working in a new project as well…

I will :slight_smile: