2016-12-27 10 views
1
これは SallaryCrudControllerにある

Laravel 5.3 - 関係配列&mailableとの問題 - 未定義のインデックス

public function remindsallary(Request $request) 
{ 
    $sallary = \App\Models\Sallary::where('isPaid', 'No')->get(); 
    $salcount = count($sallary); 
    if ($salcount > 0) { 
     Mail::to($request->user())->send(new RemindSallary($sallary)); 
     \Alert::success('Email sent with ' . $salcount . ' reminders.')->flash(); 
     return redirect('admin/sallary'); 
    } else { 
     \Alert::error('There are no open payments.')->flash(); 
     return redirect('admin/sallary'); 
    } 
} 

これは私のRemindSallary mailableです:

class RemindSallary extends Mailable 
{ 
use Queueable, SerializesModels; 

/** 
* The Sallary instance. 
* 
* @var Sallary 
*/ 
public $sallary; 
public $employee; 

/** 
* Create a new message instance. 
* 
* @return void 
*/ 
public function __construct($sallary) 
{ 
    // 
    $this->sallary = $sallary; 
} 

/** 
* Build the message. 
* 
* @return $this 
*/ 
public function build() 
{ 
    $this->employee = \App\Models\Employee::where('id', $this->sallary['employee_id'])->first(); 
    return $this->view('emails.remindsallary')->subject('Payment reminder'); 
} 
} 

そして、これは私のです思い出したビュー:

@section('content')   
@foreach ($sallary as $emp) 
<p>Name: {{ $employee->name }} :: Value: {{ $emp->value }}</p> 
@endforeach 

私はそれを起動しようとすると、私が手にエラーがある:

Undefined index: employee_id 

説明

私は行のすべてのデータを要求したとき、私は関係の罰金を印刷することができます。しかし、この場合、 'isPaid' = 'No'のすべての行のすべてのデータを要求しています。

だから、私は、これは間違っていると確信している:それは、オブジェクトがありますので

$this->employee = \App\Models\Employee::where('id', $this->sallary['employee_id'])->first(); 

が、私はそのようにそれを呼び出すことはできませんが、私は試してみましたthough.Iそれを呼び出す方法を他に知りませんsallary-> employee_idを使用しないでください。私がそれをすると、私は得る:

Undefined property: Illuminate\Database\Eloquent\Collection::$employee_id 

私はこれを修正することができますか? Employeeテーブルの従業員名を取得し、それをmailableに渡す必要があります。

答えて

1

は最後のトリックはなく直接完全に「従業員」モデルを導入する関係を呼んでいた

それを解決しました。

私は唯一のブレードビューでこれを追加する必要がありました:

@foreach ($sallary as $emp) 
<p>Name: {{ $emp->employee['name'] }} :: Value: {{ $emp['value'] }}</p> 
@endforeach 
+0

は、あなた自身の答えを受け入れることを検討してください。とても楽しいです。 – sepehr

+0

まだ**できません** – Rosenberg

関連する問題