2016-12-28 6 views
0

laravel 5.3通知システムを使用しようとしています。私はいくつかのモデルで多対多の関係を持っています。私がしなければならないことは、すべてのリクエストデータをループし、適切な人に通知を送信することです。通知メソッドはforeachループ内では機能しないようです。エラーは次のとおりです。Builder.phpライン2448で通知の使用はピボタルです

BadMethodCallException:未定義のメソッドを照らし\ Databaseへ コール\クエリー\ビルダー:: routeNotificationFor()

私が把握しようとしていますコードは次のとおりです。

public function storeHoursused(Request $request, Lessonhours $lessonhours) 
{ 
    $this->validate($request, [ 
     'date_time' => 'required', 
     'numberofhours' => 'required|numeric', 
     'comments' => 'required|max:700' 
    ]); 
    $hoursused = new Hoursused(); 
    $hoursused->date_time = $request['date_time']; 
    $hoursused->numberofhours = $request['numberofhours']; 
    $hoursused->comments = $request['comments']; 
    $lessonhours->hoursused()->save($hoursused); 
    foreach($lessonhours->players as $player){ 
      $player->users; 
      Notification::send($player, new HoursusedPosted($player->user)); 
      //$lessonhours->player->notify(new HoursusedPosted($lessonhours->player->users)); 
     } 


      return back()->with(['success' => 'Hours Used successfully added!']); 

} 

関連するデータを収集し、通知方法に渡す方法はありますか?

UPDATE:あなたの$playerモデルはIlluminate\Notifications\Notifiable traitを使用する必要がある

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 
use Collective\Html\Eloquent\FormAccessible; 
use Illuminate\Notifications\Notification; 
use Illuminate\Notifications\Notifiable; 
use Carbon\Carbon; 


class Players extends Model 
{ 
public $table = "players"; 

protected $fillable = array('fname', 'lname', 'gender', 'birthdate'); 


public function users() 
{ 
    return $this->belongsTo('App\User', 'users_id'); 
} 

public function lessonhours() 
{ 
    return $this->belongsToMany('App\Lessonhours', 'lessonhour_player',  'players_id', 'lessonhours_id') 
            ->withTimestamps(); 
} 

public function getFullName($id) 
{ 
    return ucfirst($this->fname) . ' ' . ucfirst($this->lname); 
} 

protected $dates = ['birthdate']; 
protected $touches = ['lessonhours']; 

public function setBirthdateAttribute($value) 
{ 
    $this->attributes['birthdate'] = Carbon::createFromFormat('m/d/Y', $value); 
    } 
} 

答えて

2

: プレーヤーのモデルのように見えます。

+0

です。私はそれを示すために質問を更新しました。それは私がPlayersモデルに確実に追加した最初のものの1つでした。 – wdarnellg

+0

@wdarnellgどの行?私はすぐに関連する使用が表示されません。それを最上位に含めるだけでは不十分です。モデル自体にも含める必要があります。 [関連資料](http://php.net/manual/en/language.oop5.traits.php)。 – Daedalus

+0

あなたは正しいです。 Notifiable traitをモデルに追加しました(フルパスの原因は「見つからない」というエラー)、コードが実行されます。問題は今、電子メールが送信していないということです。エラーは表示されず、レコードが保存されます。 – wdarnellg

関連する問題