kikter's avatar

livewire edit not saving

i am trying to edit a topic and its not editing after i clicked saved

class ShowTopics extends Component
{

    public $topic;
    public $showModal = false;
    public $topicId;

    protected $paginationTheme = 'bootstrap';

    protected $rules = [
        'topic.title' => 'required',
        'topic.body' => 'required',
    ];

    public function edit($topicId)
    {
        $this->showModal = true;
        $this->topicId = $topicId;
        $this->topic = Topic::find($topicId);
    }

    public function create()
    {
        $this->showModal = true;
        $this->topic = null;
        $this->topicId = null;
    }

    public function save()
    {
        $this->validate();

        if (!is_null($this->topicId)) {
            $this->topic->save();
        } else {
            Topic::create($this->topic);
        }
        $this->showModal = false;
    }

    public function close()
    {
        $this->showModal = false;
    }

    public function delete($topicId)
    {
        $topic = Topic::find($topicId);
        if ($topic) {
            $topic->delete();
        }
    }
    public function mount(Topic $topic)
    {
        $this->topic = $topic;
    }


    public function render()
    {
        return view('livewire.topic.show-topics');
    }
}

blade view


    <div
        class="@if (!$showModal) hidden @endif flex items-center justify-center fixed left-0 bottom-0 w-full h-full bg-gray-800 bg-opacity-90">
        <div class="bg-white rounded-lg w-1/2">
            <form wire:submit.prevent="save" class="w-full">
                <div class="flex flex-col items-start p-4">
                    <div class="flex items-center w-full border-b pb-4">
                        <div class="text-gray-900 font-medium text-lg">{{ $topicId ? 'Edit topic' : 'Add New topic' }}</div>
                        <svg wire:click="close"
                             class="ml-auto fill-current text-gray-700 w-6 h-6 cursor-pointer"
                             xmlns="http://www.w3.org/2000/svg" viewBox="0 0 18 18">
                            <path
                                d="M14.53 4.53l-1.06-1.06L9 7.94 4.53 3.47 3.47 4.53 7.94 9l-4.47 4.47 1.06 1.06L9 10.06l4.47 4.47 1.06-1.06L10.06 9z"/>
                        </svg>
                    </div>
                    {{-- <div class="w-full">

                        <label class="block font-medium text-sm text-gray-700" for="title">
                            Name
                        </label>
                        <input wire:model.defer="topic.title"
                               class="mt-2 text-sm sm:text-base pl-2 pr-4 rounded-lg border border-gray-400 w-full py-2 focus:outline-none focus:border-blue-400"/>
                    </div> --}}
                    <div class="py-4 border-b w-full mb-4">
                        <label class="block font-medium text-sm text-gray-700" for="title">
                        Body
                        </label>
                        <textarea wire:model.defer="topic.body"
                               class="mt-2 text-sm sm:text-base pl-2 pr-4 rounded-lg border border-gray-400 w-full py-2 focus:outline-none focus:border-blue-400"></textarea>
                    </div>
                    <div class="ml-auto">
                        <button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
                                type="submit">{{ $topicId ? 'Save Changes' : 'Save' }}
                        </button>
                        <button class="bg-gray-500 text-white font-bold py-2 px-4 rounded"
                                wire:click="close"
                                type="button"
                                data-dismiss="modal">Close
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
0 likes
2 replies
Snapey's avatar

try

        if (!is_null($this->topicId)) {
            $this->topic->save();
        } else {
            $topic = Topic::create($this->topic);
            $this->topicId = $topic->id;
        }
        $this->showModal = false;

Please or to participate in this conversation.