我用Laravel构建了一个用户邀请系统,它不起作用

时间:2018-07-04 08:50:07

标签: laravel email

我正在使用Laravel构建无法正常工作的用户邀请系统,但在向用户发送描述性图像较低的电子邮件<a href="{{ route('accept',$invite->token) }}">Click here</a> to activate!

后,在电子邮件邀请中出现了错误

我的InviteController

public function process(Request $request)
{
    // validate the incoming request data

    do {
        //generate a random string using Laravel's str_random helper
        $token = str_random();
    } //check if the token already exists and if it does, try again
    while (Invite::where('token', $token)->first());

    //create a new invite record
    $invite = Invite::create([
        'email' => $request->get('email'),
        'token' => $token
    ]);

    // send the email
    Mail::to($request->get('email'))->send(new InviteCreated($invite));

    // redirect back where we came from
    return redirect()
        ->back();
}

public function accept($token)
{
    // Look up the invite
    if (!$invite = Invite::where('token', $token)->first()) {
        //if the invite doesn't exist do something more graceful than this
        abort(404);
    }

    // create the user with the details from the invite
    User::create(['email' => $invite->email]);

    // delete the invite so it can't be used again
    $invite->delete();

    // here you would probably log the user in and show them the dashboard, but we'll just prove it worked

    return 'Good job! Invite accepted!';
}

我的图像错误 enter image description here

1 个答案:

答案 0 :(得分:0)

在传递到邮件之前先进行$ invite的dd确认您具有记录:

//create a new invite record
$invite = Invite::create([
    'email' => $request->get('email'),
    'token' => $token
]);
dd($invite);
// send the email
Mail::to($request->get('email'))->send(new InviteCreated($invite));

然后在InviteCreated上确保将$invite存储在公共属性中,以便其在刀片模板上可用:

class InviteCreated extends Mailable{

  public $invite;

  public function __construct($invite){
    $this->invite = $invite;
  }
 ...... if you use markdown then pass the `$invite`..

 public function build()
  {
    return $this
      ->markdown(your markdown mail')
      ->with([
         'invite'=>$this->invite
      ]);
  }
}