[英]Laravel 8 - Call to a member function getRealPath() on null
[英]Laravel 5.6: Call to a member function getRealPath() on string
我試圖在我的Laravel項目中發送包含文件的電子郵件,但出現此錯誤Call to a member function getRealPath() on string
這是我的store
函數:
public function store(Request $request)
{
$data = array(
'destino' => $request['destino'],
'asunto' => $request['asunto'],
'contenido' => $request['contenido'],
'a_file' => $request['a_file']
);
Mail::send('administracion.email.email_body', $data, function($message) use ($data)
{
$message->to($data['destino']);
$message->subject($data['asunto']);
$message->from(Config::get('mail.username'));
$message->attach($data['a_file']->getRealPath(), array(
'as' => 'a_file' . $data['a_file']->getClientOriginalExtension(),
'mime' => $data['a_file']->getMimeType())
);
});
return Redirect::to('email');
}
看起來$request['a_file']
給您一個字符串,而不是一個UploadedFile
實例。 您的表單支持文件上傳嗎?
使用laravelcollective Form外觀添加此內容的方法是在以下選項中傳遞'files' => true
:
{!! Form::open(['url' => route('myRoute'), 'method' => 'post', 'files' => true]) !!}
https://laravelcollective.com/docs/master/html#file-input
或者,如果您不使用Form助手,則設置如下的enctype
html屬性:
<form method="POST" action="http://example.com" enctype="multipart/form-data"></form>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.