Skip to content

fix: Laravel 13 session MessageBag 序列化兼容 (#24)#25

Open
PrintNow wants to merge 2 commits into
2.0from
fix/laravel-13-session-messagebag-compat
Open

fix: Laravel 13 session MessageBag 序列化兼容 (#24)#25
PrintNow wants to merge 2 commits into
2.0from
fix/laravel-13-session-messagebag-compat

Conversation

@PrintNow
Copy link
Copy Markdown
Owner

问题

Laravel 13 中,通过 redirect()->with() 存储的 MessageBag 对象会被序列化为数组,导致 blade 模板调用 ->get() 方法时报错:

Call to a member function get() on array
htmlspecialchars(): Argument #1 ($string) must be of type string, array given

解决方案

引入 SessionMessage 值对象替代 MessageBag,提供语义清晰的 getTitle()getMessage()getOptions() 方法。

修改文件

文件 说明
src/Support/SessionMessage.php 新增值对象
src/Support/helpers.php admin_info()admin_toastr() 改用 SessionMessage
src/Http/Controllers/ScaffoldController.php backWithSuccess()backWithException() 改用 SessionMessage
resources/views/partials/alerts.blade.php 使用对象方法替代 Arr::get()
resources/views/partials/toastr.blade.php 使用对象方法替代 Arr::get()
resources/views/partials/exception.blade.php 使用 MessageBag::first() 并缓存变量

兼容性

兼容 Laravel 10、11、12、13。

Closes #24

In Laravel 13, MessageBag objects stored via redirect()->with() are
serialized to arrays, causing "Call to a member function get() on array"
errors in blade templates.

Introduce SessionMessage value object as a clean replacement, providing
getTitle(), getMessage(), and getOptions() methods.

Closes #24
@PrintNow PrintNow force-pushed the fix/laravel-13-session-messagebag-compat branch from 610486a to 21c019a Compare May 26, 2026 14:11
Remove unused fromArray() and toArray() methods. SessionMessage intentionally
does not implement Arrayable to prevent Laravel 13 from auto-converting it to
an array via redirect()->with(). Having toArray() posed a theoretical risk if
future Laravel versions detect the method instead of the interface.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: Laravel 13 兼容性 - session 中 MessageBag 被序列化为数组

1 participant