Laravel 5.6: Call to a member function getRealPath () on string

0

I am trying to send an email from my Laravel project. Upon entering the store I get this error:

  

Call to a member function getRealPath () on string

The email is sent with an attachment, when it is sent without taking into account the attached file, it is sent well.

Here I put the driver:

<?php

namespace proyecto\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Storage;
use Mail;
use Config;
use Illuminate\Validation\Rule;

class EmailController extends Controller {
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

     /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
     public function index() {       
         return view('administracion.email.email')
                ->with('method','POST');
     }

     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'); 
     }
}
    
asked by Kinafune 10.10.2018 в 00:36
source

1 answer

0

is interpreting the $ request ['a_file'] as the string, instead of a file object, that's why it tells you to call the getRealPath () method in a string.

Searching in the laravel manual link , says that to fetch a file from the request You must sign in this way:

$file = $request->file('photo');

Therefore, your code should be something like this:

 $data = array(
     'destino'    => $request['destino'],
     'asunto'     => $request['asunto'],
     'contenido'  => $request['contenido'],
     'a_file'     => $request->file('a_file')
 );

With this now $ data ['a_file'] should be a file object that will have its method getRealPath ()

Emphasize that, in case you return null, you will have an error again, this time to call getRealPath () in null. I recommend that you put in an if the code of insertion of the file:

if(!null($data['a_file']) {
  $message->attach(
      $data['a_file']->getRealPath(), 
      array(
          as'=> 'a_file'.$data['a_file']->getClientOriginalExtension(),
      'mime'  => $data['a_file']->getMimeType()
     ) 
  );
}
    
answered by 10.10.2018 в 01:16