Laravel 5.6:在字符串上调用成员函数getRealPath()

时间:2018-10-09 23:12:38

标签: php laravel laravel-5

我正在尝试在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');

}

1 个答案:

答案 0 :(得分:2)

似乎$request['a_file']在给您一个字符串,而不是UploadedFile实例。您的表单支持文件上传吗?

使用laravelcollective窗体外观添加此内容的方法是在以下选项中传递'files' => true

{!! Form::open(['url' => route('myRoute'), 'method' => 'post', 'files' => true]) !!}

https://laravelcollective.com/docs/master/html#file-input

或者,如果您不使用表单帮助器,请像这样设置enctype html属性:

<form method="POST" action="http://example.com" enctype="multipart/form-data"></form>