I am trying to send an email to the registered uses but when i tried to run my code i got this error in the network tab

"message": "htmlspecialchars() expects parameter 1 to be string, object given (View: C:\\xampp\\htdocs\\laravel Projects\\bank\\iscbank\\resources\\views\\emails\\welcome.blade.php)",
    "exception": "ErrorException",

heres my code in the welcome.blade.php

<html lang="en-US">
<head>
    <meta charset="text/html">
</head>
<body>
    Name: {{$name}}
    Password:
    {{$password}}
</body>
</html>

here is my RegisterController.php code

    <?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Auth\Events\Registered;
use App\Mail\WelcomeMail;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;

class RegisterController extends Controller{
    protected function genPass(){
        $limit= 8;
        $chars= '[email protected]#$%^&*()_+=-[]{}\|ABCDEFGHIJKLMNOPQRSTUVXWYZ';
        $chararray= str_split($chars);
        $gen=array();
        for($i=0;$i<$limit;$i++){
            $index=rand(0,strlen($chars)-1);
            $gen[$i]= $chararray[$index];
        }
        return implode($gen);
    }
    protected function genAutKey(){
        return bin2hex(random_bytes(8));
    }
    protected function testFunction() {

     }

    use RegistersUsers;

    protected $redirectTo = '/home';
    private $genPass;
    private $genAutKey;

    public function __construct(){  
        $this->genPass = $this->genPass();
        $this->genAutKey = $this->genAutKey();      
        $this->middleware('admin');
    }

    protected function validator(array $data){
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'phoneNumber' => 'required|numeric|unique:users',
        ]);
    }

    protected function create(array $data){        
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],                
            'Authkey' => $this->genAutKey, 
            'password' => bcrypt($this->genPass),
            'phoneNumber' => $data['phoneNumber'],
            'address' => $data['address'],
        ]);
    }

     public function register(Request $request)  {       
        $validation = $this->validator($request->all());
        if ($validation->fails())  {  
            return response()->json(['errors'=>$validation->errors()->toArray()]);
        }
        else{            
            $this->create($request->all()); 

            // Mail::to($request->input('email'))->send(new WelcomeMail($sentt));
            $mail = Mail::send(['html' => 'emails.welcome'], array(
                'name' => $request->input('name'), 
                'email' => $request->input('email'), 
                'Authkey' => $this->genAutKey, 
                'password' => $this->genPass,
                ), function($message){
                    $message->from(Auth::user()->email, Auth::user()->name);    
                    $message->to(Input::get('email'))->subject(Input::get('subject'));    
            });
            return response()->json(['success'=>'Customer registerd successfully']);
        }
    }
}

Pls what am i doing wrong, i have tryed alot and even searched online but could not ind anything usefull that will help me

1 Answers

2
Sethu On

I think it should be $this->genPass not $this->$genPass on your construct method

same for $this->$genAutKey, it should be like $this->genAutKey