Laravel htmlspecialchars() 期望参数 1 为字符串,object 在我的项目中给出?
Laravel htmlspecialchars() expects parameter 1 to be string, object given in my project?
所以我正在尝试编写一个简单的网站表单。但是它有这个 htmlspecialchars 错误。
我试过制作{{ $message }},但没成功。有同样的错误。
这是我的控制器:
<?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');
}
}
这是我的 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>
这是我的 contact-message.blade.php
{{ $message }}
我也试过了
{{dd($message)}}
但是没用。
请帮忙。
你应该试试这个:
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 = 'john@example.com';
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');
}
}
只需将控制器中的数组键从 message
更改为 messages
,如下所示:
$data = array(
'messages' => $request->message
);
并且在 blade 中打印为 {{$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.
检查此 link 中的注释:http://laravel.com/docs/5.0/mail#basic-usage
所以我正在尝试编写一个简单的网站表单。但是它有这个 htmlspecialchars 错误。
我试过制作{{ $message }},但没成功。有同样的错误。
这是我的控制器:
<?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');
}
}
这是我的 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>
这是我的 contact-message.blade.php
{{ $message }}
我也试过了 {{dd($message)}}
但是没用。
请帮忙。
你应该试试这个:
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 = 'john@example.com';
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');
}
}
只需将控制器中的数组键从 message
更改为 messages
,如下所示:
$data = array(
'messages' => $request->message
);
并且在 blade 中打印为 {{$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.
检查此 link 中的注释:http://laravel.com/docs/5.0/mail#basic-usage