Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vmutius's avatar

Livewire3: Edited attributes are not stored in DB plus user gets logged out

I guess I need some more eyes and brains. I create an app where I want to be able to edit blogposts. I have written this as a livewire component. My Blade file resources/views/livewire/admin/post/edit.blade.php looks like this:

My livewire component looks like this:

and my PostForm looks like this:

When I try to save the changes of the blogpost attributes in the app, I get logged out and no changes are stored in the DB. Also the save() methode does not seems to be called, since I do not see any output in ray().

What am I doing wrong?

0 likes
2 replies
LaryAI's avatar
Level 58

The issue you're experiencing, where the user gets logged out and changes are not stored in the database, could be due to a few potential problems. Let's go through some troubleshooting steps and solutions:

  1. CSRF Token: Ensure that the CSRF token is correctly included in your form. However, since you're using Livewire, it should handle CSRF tokens automatically. Double-check that your session is not expiring or being invalidated for some reason.

  2. Session Configuration: Check your session configuration in config/session.php. Ensure that the session driver is correctly set and that the session is not being cleared or expired prematurely.

  3. Middleware: Ensure that your Livewire component is not being affected by middleware that might be logging out the user. Check your web middleware group and any custom middleware that might be interfering.

  4. Livewire Lifecycle: If the save() method is not being called, it might be due to a JavaScript error or a problem with the Livewire lifecycle. Open your browser's developer tools and check the console for any JavaScript errors.

  5. Debugging: Add some debugging statements to ensure that the mount() method is being called correctly and that the form data is being set as expected. You can use ray() or dd() to inspect the data.

  6. File Uploads: Since you're using file uploads, ensure that your php.ini settings allow for the file sizes you're working with. Also, make sure that the newImage property is being handled correctly in your save() method.

  7. Validation: Ensure that your validation rules are not causing the form submission to fail silently. You can add a try-catch block around your validation to catch any exceptions and log them.

Here's a checklist of things to verify and a potential solution:

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

        if ($this->newImage) {
            $this->image = $this->newImage->store('images', 'public');
        }

        if ($this->post) {
            $this->update();
        } else {
            $this->store();
        }

        session()->flash('message', 'Post erfolgreich aktualisiert.');
        return redirect()->route('admin_post_index');
    } catch (\Exception $e) {
        // Log the exception or use ray() to debug
        ray($e->getMessage());
        session()->flash('error', 'An error occurred while saving the post.');
    }
}
  • Check for JavaScript Errors: Open the browser console and ensure there are no JavaScript errors that might be preventing Livewire from functioning correctly.

  • Session Storage: Ensure that your session storage is correctly configured and that the session is not being cleared unexpectedly.

  • File Upload Handling: If you're uploading a new image, ensure that the file is being processed and stored correctly. You might need to handle the file upload separately if it's causing issues.

By following these steps, you should be able to identify the root cause of the problem and resolve the issue with your Livewire component.

Phread's avatar

In my previous app when I was dealing with attachments I needed to do the following in the blade:

<form .......  enctype="multipart/form-data">

When enctype="multipart/form-data" was not present, I would receive an error-500 and would get logged out.

Please or to participate in this conversation.