2017-08-04 110 views
1

我試圖建立電子郵件在Laravel的,我有這個麻煩,我想發送電子郵件每個星期一因此我想觸發它​​作爲一個命令,並安排它(除非有)這裏是我的電子郵件:Laravel電子郵件,傳遞變量

<?php 

namespace App\Mail; 

use App\Event; 
use Illuminate\Bus\Queueable; 
use Illuminate\Contracts\Queue\ShouldQueue; 
use Illuminate\Mail\Mailable; 
use Illuminate\Queue\SerializesModels; 

    class MondayEmails extends Mailable 
    { 
     use Queueable, SerializesModels; 

     /** 
     * Create a new message instance. 
     * 
     * @return void 
     */ 
     public function __construct() 
     { 
      $events = Event::limit(5) 
       ->orderBy('title') 
       ->get(); 
      return $events; 
     } 

     /** 
     * Build the message. 
     * 
     * @return $this 
     */ 
     public function build() 
     { 
      return $this->from('[email protected]') 
         ->view('emails.mondayEmail'); 
     } 
    } 

在這一點上$ 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 \資源\意見\電子郵件\ 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

它實際工作!我剛剛有語法錯誤thansk –

+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()提供的無效參數() –