zack zack
zack zack

Reputation: 207

Sending Mail with Laravel

i'am trying to send a Mail with laravel after the registration , basically it's working , but when i added the name of the doctor into the Mail i got this error : Trying to get property 'Nom' of non-object , probably it doesn't know the "Name" of the doctor. Thank you for your Help

The Doctor Controller :

   public function ajouter (Request $request) {

        $this->validate($request, [

            'n' => 'required', 
            'p' => 'required' ,
            'adresse' => 'required',
            'mail' => 'required|email',
            'mdp' => 'required|min:4',
            'spec' => 'required',
            'region' => 'required',
            'ville'=>'required',
            'photo'=> 'image|nullable|max:3999'
        ]);

        $medecin= new doc() ;

        $medecin->Nom= $request->input('n');
        $medecin->Prénom =$request->input('p');
        $medecin->Spécialité=$request->input('spec');
        $medecin->Adresse_Cabinet=$request->input('adresse');
        $id_ville=$request->input('region');
        $medecin->Ville=vil::find($id_ville)->Ville;
        $medecin->Délégation=$request->input('ville');

        if($request->hasFile('photo')){
            // Get filename with th extension
        $file = $request->file('photo');
        $extension=$file->getClientOriginalExtension();
        $fileNameToStore=time().'.'.$extension;
        $file->move('assets/img/uploads/',$fileNameToStore);
        }
        else {
            $fileNameToStore =('noimage.png');
        }
        $medecin->photo=$fileNameToStore;
        $medecin->Login=$request->input('mail');
        $medecin->Password=Hash::make($request->input('mdp'));
        $medecin->save(); 

        Mail::to($request->input('mail'))->send(new WelcomeDoctorMail($medecin));

        return redirect()->back()->withSuccess('medecin ajouter' ) ; 
    }

The Mail controller :

  public $medecin;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(doc $medecin)
    {
        $this->doc = $medecin ;
    }
public function build()
{
    return $this->markdown('emails.welcome-doctor');
}

}

The Mail view :

@component('mail::message')
# Bienvenue

Merci {{$medecin->Nom}} de vous etes inscrit sur notre application.


Cordialement
Thanks,<br>
{{ config('app.name') }}
@endcomponent

Upvotes: 0

Views: 68

Answers (2)

Andrew Larsen
Andrew Larsen

Reputation: 1267

You can use the with() function with an array as the first argument to specify what you want to access in the mail markup when returning from the build() function.

Like this:

public function build()
{
    return $this->markdown('emails.welcome-doctor')
    ->with([
        'medecin' => $this->doc
    ]);
}

EDIT: According to the documentation (https://laravel.com/docs/7.x/mail#view-data) you can access the variable directly if it's set to public in the class that extends Mailable. So you should be able to access $doc within the markup (not $medecin).

EDIT 2: I now see that you have defined public $medecin but you are trying to save the constructors parameter value in $this->doc which is not defined in the class that extends Mailable class nor used in the markdown. You could solve all your problems according to the documentation, by just changing $this->doc = $medecine to $this->medecine = $medecine.

Upvotes: 1

amr degheidy
amr degheidy

Reputation: 317

first make sure the name inserted correctly into database , if it's correct try to access it like an array

@component('mail::message')
# Bienvenue

Merci {{$medecin['Nom']}} de vous etes inscrit sur notre application.


Cordialement
Thanks,<br>
{{ config('app.name') }}
@endcomponent

Upvotes: 0

Related Questions