2017-08-04 12 views
1

私はLaravelで電子メールを作成しようとしていますが、これに問題があります。毎週月曜日に電子メールを送信したいので、コマンドとして起動してスケジュールしますより良い方法は?)私の電子メールはここにあります:Laravel電子メール、パス変数

この時点で、$ eventsは私にコレクションを返します。

@foreach ($events as $event) 
    {{ $event->title }} 
@endforeach 

とコマンド:

は、ここに私のメールビューの

<?php 

namespace App\Console\Commands; 

use App\Event; 
use App\Mail\MondayEmails; 
use Illuminate\Console\Command; 
use Illuminate\Http\Request; 
use Illuminate\Support\Facades\Mail; 

class MondayEmail extends Command 
{ 
    /** 
    * The name and signature of the console command. 
    * 
    * @var string 
    */ 
    protected $signature = 'email:monday'; 

    /** 
    * The console command description. 
    * 
    * @var string 
    */ 
    protected $description = 'Monday Events being send!'; 

    /** 
    * Create a new command instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     parent::__construct(); 
    } 

    /** 
    * Execute the console command. 
    * 
    * @return mixed 
    */ 
    public function handle(Request $request) 
    { 
     Mail::to('[email protected]')->send(new MondayEmails()); 

    } 
} 

と私はそれを実行したときに私が取得:

[ErrorException]未定義の変数:イベント(ビュー: C:¥xampp¥htdocs¥liveandnow¥resources¥views¥emails¥mondayEmail.blade.php)

[ErrorException]未定義の変数:イベント

どうすれば解決できますか?これは正しいアプローチですか?それとも別の方法でやるのか。

答えて

1

コンストラクタが返されないため、クラス内の他の関数にアクセスするためのプロパティを設定できます。 MondayEmailsクラスで次のコードを試してみると、コンストラクタで返された電子メールにアクセスできます。

protected $events; 
public function __construct() 
{ 
    $this->events = Event::limit(5) 
     ->orderBy('title') 
     ->get(); 
} 

public function build() 
{ 
    $events = $this->events; 
    return $this->from('[email protected]') 
       ->view('emails.mondayEmail', compact('events')); 
} 
+0

まだ動作しません/ [ErrorException] foreach()の引数が無効です –

+0

実際に動作します!私はちょうど構文エラーがありました –

+0

@PrzemekWojtas良い仕事! – mbozwood

1

私はあなたがこれを試してみてくださいと思う:

protected $events; 
public function __construct() 
{ 
    $this->events = Event::limit(5) 
     ->orderBy('title') 
     ->get(); 
} 

public function build() 
{ 
    $events = $this->events; 
    return $this->from('[email protected]') 
       ->view('emails.mondayEmail') 
       ->->with([ 
       'events' => $events, 
       ]); 
} 

はあなたのためにこの作品を願っています!

+0

これらの解決策の両方で私は次のようになります。[ErrorException] 未定義のプロパティ:App \ Mail \ MondayEmails :: $ events –

+0

@PrzemekWojtasそれをチェックさせてください –

+0

保護された$イベントを忘れました。今すぐ[ErrorException]が発生しています foreach()の引数が無効です –

関連する問題