【问题标题】:Laravel 5.1 Mailer Trying to get property of non-objectLaravel 5.1 Mailer 试图获取非对象的属性
【发布时间】:2015-10-08 17:00:49
【问题描述】:

在 Mailer.php 第 33 行获取 ErrorException:试图获取非对象的属性

我的 Mailer.php:

<?php

namespace App\Mailers;

use Illuminate\Contracts\Mail\Mailer as Mail;

abstract class Mailer
{
    /**
     * @var Mail
     */
    protected $mail;

    /**
     * @param Mail $mail
     */
    public function __construct(Mail $mail)
    {
        $this->mail = $mail;
    }

    /**
     * @param $to
     * @param $subject
     * @param $from
     * @param $view
     * @param null $data
     */
    public function mailTo($to, $subject, $from, $view, $data = null)
    {
        $this->mail->send($view, $data, function($message) use ($to, $from, $subject)
        {
            $message->to($to->email)->subject($subject)->from($from);
        });
    }
}

我的 SiteMailer.php 扩展了我的 Mailer.php 抽象类

<?php

namespace App\Mailers;

class SiteMailer extends Mailer
{
    /**
     * @param $data
     */
    public function sendEmailMessageToSupport($data)
    {
        $from = env('MAIL_NOREPLY', 'SUPPORT');
        $to = env('MAIL_NOREPLY', 'SUPPORT');
        $subject = 'Activate Your Account';
        $view = 'auth.emails.support';

        $this->mailTo($to, $subject, $from, $view, $data);
    }
}

还有我的 SupportController.php

<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use App\Mailers\SiteMailer;
use App\Http\Controllers\Controller;
use App\Http\Requests\SupportRequest;

class SupportController extends Controller
{
    public function create()
    {
        return view('pages.support');
    }

    public function store(SupportRequest $request, SiteMailer $mail)
    {
        $mail->sendEmailMessageToSupport($request->all());

        return redirect()->back()->with('alert-success', 'Thanks for contacting us!');
    }
}

我的 SupportRequest FormRequest

<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class SupportRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required',
            'email' => 'required|email',
            'message_content' => 'required',
        ];
    }
}

然后是电子邮件视图

<p>
    A prospective customer named {{ $name }} <small>{{ $email }}</small>
    has submitted an inquiry through Our Site.
</p>

<p>
    {{ $message_content }}
</p>

找不到问题所在。

【问题讨论】:

    标签: php laravel-5 laravel-5.1


    【解决方案1】:

    我认为问题出在这里:

     $message->to(**$to->email**)->subject($subject)->from($from);
    

    $to 是一个电子邮件地址的字符串,由此判断:

     $to = env('MAIL_NOREPLY', 'SUPPORT');
    

    所以只需去掉-&gt;email 位,因为字符串没有属性/属性,例如:

     $message->to($to)->subject($subject)->from($from);
    

    【讨论】:

      猜你喜欢
      • 2015-10-15
      • 1970-01-01
      • 2018-07-14
      • 2015-09-22
      • 2017-03-20
      • 2014-03-25
      • 2015-01-25
      • 2014-06-23
      相关资源
      最近更新 更多