Laravel htmlspecialchars() expects parameter 1 to be string, object given in my project?

calvinerico picture calvinerico · Feb 12, 2019 · Viewed 16k times · Source

So i'm trying to code a simple website form. But it has this htmlspecialchars error.

I've tried to make {{ $message }} but it didn't work. has the same error.

this is my controller :

<?php
namespace App\Http\Controllers;
use Mail;
use Illuminate\Http\Request;
class ContactMessageController extends Controller
{
public function create()
    {
        return view('form');
    }

public function store(Request $request)
{
    $this->validate($request, [
        'name' => 'required',
        'email' => 'required|email',
        'address' => 'required',
    ]);

    Mail::send('emails.contact-message', [
        'message' => $request->message
    ], function($mail) use($request) {
        $mail->from($request->email, $request->name);

        $mail->to('[email protected]')->subject('Contact message');
    });

        return redirect()->back()->with('flash_message', 'thanks');
    }
}

and this is my blade

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Customer Details</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.0/css/bootstrap.min.css" integrity="sha384-PDle/QlgIONtM1aqA2Qemk5gPOE7wFq8+Em+G/hmo5Iq0CCmYZLv3fVRDJ4MMwEA" crossorigin="anonymous">
<style>
    .invalid-feedback {
        display: block;
    }
</style>
</head>
<body>
<div class ="container">
    <h1>Customer Form</h1>
        @if (Session::has('flash_message'))
            <div class="alert alert-success">{{ Session::get('flash_message') }}</div>
        @endif
    <form method="post" action="{{ route('contact.store') }}">
    {{ csrf_field() }}
        <div class="form-group">
            <label>Full Name : </label>
            <input type="text" class="form-control" name="name">
            @if ($errors->has('name'))
                <small class="form-text invalid-feedback">{{ $errors->first('name') }}</small>
            @endif
        </div>

        <div class="form-group">
            <label>Email : </label>
            <input type="text" class="form-control" name="email">
            @if ($errors->has('email'))
                <small class="form-text invalid-feedback">{{ $errors->first('email') }}</small>
            @endif
        </div>

        <div class="form-group">
            <label>Address : </label>
            <textarea name="address" class ="form-control"></textarea>
            @if ($errors->has('address'))
                <small class="form-text invalid-feedback">{{ $errors->first('address') }}</small>
            @endif
        </div>

        <div class="form-group">
            <label>Message : </label>
            <textarea name="message" class ="form-control"></textarea>
            @if ($errors->has('message'))
                <small class="form-text invalid-feedback">{{ $errors->first('message') }}</small>
            @endif
        </div>

        <button class="btn btn-primary">Submit</button>


    </form>
</div>

</body>
</html>

and this is my contact-message.blade.php

{{ $message }}

also i've tried {{dd($message)}}

but it didnt work.

please help.

Answer

Md Riadul Islam picture Md Riadul Islam · Feb 12, 2019

Just change the array key from message to messages in your controller like below:

$data = array(
        'messages' => $request->message
        );

and also in the blade print it as {{$messages}}

A $message variable is always passed to e-mail views, and allows the inline embedding of attachments. So, it is best to avoid passing a message variable in your view payload.

Check the note in this link: http://laravel.com/docs/5.0/mail#basic-usage