Laravel htmlspecialchars() expects parameter 1 to be string, object given
When you encounter the error htmlspecialchars() expects parameter 1 to be string, object given in a Laravel application, it usually stems from a mismatch between how Blade renders variables and how the underlying PHP function handles data types. In the scenario provided, the issue isn't with the form itself or the session flash message logic; it is entirely contained within the email template.
The error occurs because the {{ $message }} syntax in your contact-message.blade.php file attempts to output a variable named $message. However, looking at your controller's store method, you are passing $request->message into the view data array:
Mail::send('emails.contact-message', [
'message' => $request->message
], function($mail) use($request) {
// ...
});
At first glance, this seems correct. The Request object has a message property that returns the raw string value entered in the textarea. So why does PHP complain about an "object"?
The culprit is almost certainly the Laravel helper function dd() (dump and die) mentioned in your troubleshooting attempts. If you run dd($message) inside the controller or view to debug, it stops script execution immediately. While this helps identify that $message is a string, it prevents the email from sending. However, if you are seeing the error during the send attempt, check where the data originates.
If your input field name="message" is empty or if the validation rules inadvertently cast the data incorrectly in older Laravel versions, the variable might not be a string at all. But there is a more subtle and common cause: the dd() function itself. If you have dd($message) inside the controller code snippet provided (which isn't shown in the snippet but implied by your text "i've tried {{dd($message)}}"), the script halts before Mail::send ever completes.
Wait, looking closer at the specific error message htmlspecialchars() expects parameter 1 to be string, this function is called internally by Blade when rendering {{ $variable }}. If the value passed to $variable in the view context is not a string, the crash happens instantly during rendering.
In your controller, $request->message returns the raw input. This should be a string. However, if you are using a custom trait or an older version of Laravel where dd() behaves differently, or if you accidentally passed an object (like the entire Request instance) instead of the specific property, this error triggers.
To resolve this without relying on dd() which halts execution, use the dump method from the Illuminate\Support\Arr helper or simply rely on Laravel's built-in type safety. But the most robust fix involves ensuring you are explicitly casting the value if there is any doubt:
Mail::send('emails.contact-message', [
'message' => (string) $request->message
], function($mail) use($request) {
// ...
});
Casting $request->message to a string guarantees that htmlspecialchars() receives the correct data type, preventing the crash regardless of what the input contained.
Additionally, ensure your validation rules are strict enough to prevent unexpected empty strings if that is causing issues in your specific environment, though an empty string is still a valid string. The error specifically complains about an object, so the casting (string) is the definitive fix if the source data somehow retains object properties due to a custom accessor or trait.
Finally, verify that you aren't accidentally passing the whole request object into the view array by mistake elsewhere in your codebase. If you are using dd($message) inside the controller to debug, remember that this terminates the script immediately after dumping the output. You will never see the email sent or any subsequent errors because the application lifecycle stops there. Remove debugging calls like dd() from production controllers to ensure the mailer executes fully and allows you to see the actual result of the Mail::send operation.