Laravel htmlspecialchars()期望参数1是字符串,在我的项目中给定的对象? [英] Laravel htmlspecialchars() expects parameter 1 to be string, object given in my project?
问题描述
所以我正在尝试编写一个简单的网站表单.但是它有这个htmlspecialchars错误.
So i'm trying to code a simple website form. But it has this htmlspecialchars error.
我尝试制作{{$ message}},但是没有用.有相同的错误.
I've tried to make {{ $message }} but it didn't work. has the same error.
这是我的控制器:
<?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('john@example.com')->subject('Contact message');
});
return redirect()->back()->with('flash_message', 'thanks');
}
}
这是我的刀刃
<!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>
这是我的contact-message.blade.php
and this is my contact-message.blade.php
{{ $message }}
我也尝试过{{dd($ message)}}
also i've tried {{dd($message)}}
但是没有用.
请帮助.
推荐答案
只需在控制器中将数组键从 message
更改为 messages
,如下所示:
Just change the array key from message
to messages
in your controller like below:
$data = array(
'messages' => $request->message
);
,并在刀片中将其打印为 {{$$ messages}}
and also in the blade print it as {{$messages}}
$ message变量始终传递到电子邮件视图,并允许附件的嵌入式嵌入.因此,最好避免通过您的视图有效负载中的message变量.
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.
在此链接中查看注释: http://laravel.com/docs/5.0/mail#basic-usage
Check the note in this link: http://laravel.com/docs/5.0/mail#basic-usage
这篇关于Laravel htmlspecialchars()期望参数1是字符串,在我的项目中给定的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!