Laravel htmlspecialchars() 期望参数 1 是字符串,我的项目中给出的对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54645343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel htmlspecialchars() expects parameter 1 to be string, object given in my project?
提问by calvinerico
So i'm trying to code a simple website form. But it has this htmlspecialchars error.
所以我正在尝试编写一个简单的网站表单。但它有这个 htmlspecialchars 错误。
I've tried to make {{ $message }} but it didn't work. has the same error.
我试图制作 {{ $message }} 但它没有用。有同样的错误。
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
这是我的 contact-message.blade.php
{{ $message }}
also i've tried {{dd($message)}}
我也试过 {{dd($message)}}
but it didnt work.
但它没有用。
please help.
请帮忙。
回答by Md Riadul Islam
Just change the array key from message
to messages
in your controller like below:
只需将控制器中的数组键从 更改message
为messages
,如下所示:
$data = array(
'messages' => $request->message
);
and also in the blade print it as {{$messages}}
并在刀片中将其打印为 {{$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.
$message 变量始终传递给电子邮件视图,并允许内联嵌入附件。因此,最好避免在视图负载中传递消息变量。
Check the note in this link: http://laravel.com/docs/5.0/mail#basic-usage
检查此链接中的注释:http: //laravel.com/docs/5.0/mail#basic-usage
回答by AddWeb Solution Pvt Ltd
You should try this:
你应该试试这个:
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
'email' => 'required|email',
'address' => 'required',
]);
$fromName = $request->name;
$subject = "MailSent";
$data = array(
'message' => $request->message
);
$fromEmail = $request->email;
$toName = 'test';
$toEmail = '[email protected]';
Mail::send('emails.contact-message', $data, function($message) use ($toEmail, $toName, $fromEmail, $fromName, $subject){
$message->from($fromEmail, $fromName);
$message->to($toEmail, $toName);
$message->subject($subject);
});
return redirect()->back()->with('flash_message', 'thanks');
}
}