Form with model binding

What seems to be the problem:
I have a form display data related to a model. Have selected all the properties from the model except the ID
When saving the form, the model does not get updated.
I could select the ID but I would rather not as I would prefer to have the value kept out of sight.

Are you using the latest version of Livewire:
yep

Schema::create('static_client_contents', function (Blueprint $table) {
            $table->id();
            $table->foreignId('client_id');

            //contact details
            $table->string('tel', 20)->nullable();
            $table->string('email', 255)->nullable();
            ...

    public StaticClientContent $myModel;

    protected $rules = [
        'myModel.tel' => 'nullable',
        'myModel.email' => 'nullable|email',
        ...
    ];

    protected $messages = [];

    public function mount()
    {

//**
   client_id   is unique foreign KEY that allows to pull the correct row
**/
        $this->myModel = StaticClientContent::select('tel', 'email', ... )  
                                    ->where('client_id', session()->get('adminClientSelectorSelected') )
                                    ->first();
    }

    public function store()
    {

        $this->validate($this->rules, $this->messages);

        $this->myModel->update(); //does not do anything as ID is not selected in mount
        $this->myModel->where('client_id', '=', session()->get('adminClientSelectorSelected') )->update(); //does not work

        dd($this->myModel);
    }

    public function render()
    {
        return view('livewire.admin.client-static-content');
    }

Is there a way to do this without selecting the row in mount? I tried to assign the ID manually before updating and that did not work,

Hey, @Frederico
The first thing, that you are not passing anything into the update() method and make sure the session has a value first. So, you can dd into the session first when you are trying to update your model and after that make sure you are retrieving the needed collection (the values you are trying to update).

1 Like