Laravel新错误 - 未定义变量:捐赠者,方法'表'在\ Illuminate \ .. \ DB

时间:2017-05-21 19:24:40

标签: php laravel email laravel-5 laravel-5.4

我可以在输入后获取特定电子邮件的数据。但是,这只能在进入该页面后进行。但现在,在我这样做之前,我需要将用户输入数据库并向他发送电子邮件。输入信息并提交后,我收到一条错误,告诉"未定义的变量:捐赠者"。尽管如此,用户仍然会被添加到数据库中,但我无法进入下一页并收到错误,因为它还有一个函数,它发送一封电子邮件(receipt.blade.php),因为它发出错误,因为"施主"未定义。

我的控制器有这些:

use App\Donor;
use App\Video;
use Illuminate\Http\Request;
use DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Mail;

该功能为

public function getUserReceipts(Request $data){
    $email = $data->email;

    $donor = Donor::where('email', $email)->get(); 

   return view('emails.receipt')->with(compact('donor'));
}

receipt.blade.php页面具有sandeesh的答案中建议的功能

@foreach($donor as $value)
    {{ $value->first_name }}
    {{ $value->last_name }}
    {{ $value->amount_donated }}
@endforeach

发送电子邮件的功能是:

public function thankyoupage(Request $data, Mailer $mailer){
$donor = Donor::create([
            'first_name'=> $data->first_name,
            'last_name' => $data->last_name,
            'email' => $data->email,
            'video_count' => $video_count,
            'amount_donated' => $data->amount_donated,
});

$mailer
            ->to($data->input('email'))
            ->send(new \App\Mail\MyMail(($data->input('first_name')),($data->input('last_name')),
                ($data->input('amount_donated'))));

return redirect()->action('PagesController@thank1');
    }

我的mail.php如下:

class MyMail extends Mailable
{
    use Queueable, SerializesModels;

    public $first_name, $last_name, $amount_donated;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($first_name,$last_name,$amount_donated)
    {
        $this->first_name = $first_name;
        $this->last_name = $last_name;
        $this->amount_donated = $amount_donated;

    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from('myemailid12341@gmail.com')
            ->view('emails.receipt');
    }
}

2 个答案:

答案 0 :(得分:1)

在顶部添加

use DB; 

或者

$donor = Illuminate\Support\Facades\DB::table..

使用别名并导入它或完整路径。这可能也会发生,因为你的config / app.php中的别名不合适。

答案 1 :(得分:0)

use Illuminate\Support\Facades\DB;替换为use DB;

这是你的主要问题。您在循环中返回视图。这会导致为第一个捐赠者呈现视图,其余部分将被忽略。您需要将捐赠者传递给视图,然后在视图中循环以显示它们。这样做。

控制器

public function getUserReceipts(Request $data)
{
    $email = $data->email;

    $donors = Donor::where('email', $email)->get();

    return view('emails.receipt')->with(compact('donors'));
}

在你看来

@foreach($donors as $donor)
    {{ $donor->first_name }}
    {{ $donor->last_name }}
    {{ $donor->amount_donated }}
@endforeach