Search code examples
phplaravellaravel-livewirelaravel-10

Laravel livewire data not binding to input fields


why I can't bind or display data from the database going to input text field. I already add the rules just like what other said but still not luck. Please help, I stack on this part.

By the way I am using modal from question table CRUD.

LIVEWIRE COMPONENT

class ViewPromo extends Component
{

    public $questions;
    public Promo $promo;
    public Question $question;

    protected $rules = [
        'question.question_title' => 'required',
        'question.question_type' => 'required',
    ];

    public function editQuestion(Question $question) {
        $this->question = $question;
    }

    public function mount(Promo $promo) {
        $this->promo = $promo;
        $this->questions = $this->promo->questions()->get();
    }

    public function render()
    {
        return view('livewire.admin.promos.view-promo')->extends('layouts.app')->section('contents');
    }
}

LIVEWIRE BLADE FILE

<div>
    <x-label for="question_title">Title</x-label>
    <x-input-text wire:model.defer="question.question_title" id="question_title"></x-input-text>
</div>

<div>
    <x-label for="question_type">Type</x-label>
    <x-select wire:model.defer="question.question_type" id="question_type">
        <option value="single_select" @if($question->question_type == 'single_select') selected @endif>Single select</option>
        <option value="multiple_select" @if($question->question_type == 'multiple_select') selected @endif>Multiple select</option>
    </x-select>
</div>

BUTTON FROM THE LIST

<x-button wire:click="editQuestion({{ $question->id }})" data-modal-target="edit-default-modal" data-modal-toggle="edit-default-modal" >Edit</x-button>

I want to display record from the database going to the input fields.


Solution

  • Do you happen to be using Livewire 3? Because, if you read the docs, Livewire 3 no longer uses Eloquent Model Binding by default. If you want to use that behaviour you need to enable it (in config/livewire.php):

    'legacy_model_binding' => true,