2016-12-28 62 views
0

我想使用laravel 5.3通知系统。我在几个模型上有多对多的关系。我需要做的是循环所有的请求数据并向适当的每个人发送一个通知。看起来通知方法在foreach循环中不起作用。错误是:使用通知是关键

BadMethodCallException在Builder.php线2448: 调用未定义的方法照亮\数据库\查询\生成器:: 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: 的球员模型是这样的:

<?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

$player模型需要使用Illuminate\Notifications\Notifiable trait

+0

它的确如此。我刚刚更新了这个问题以表明这一点。这是我确定要添加到玩家模型中的第一件事情之一。 – wdarnellg

+0

@wdarnellg在哪一行?我不立即看到相关用途。将它包含在顶部是不够的,您还必须将其包含在模型本身中。 [相关阅读](http://php.net/manual/en/language.oop5.traits.php)。 – Daedalus

+0

你说得对。我向模型中添加了使用Notifiable特征(完整路径导致'Not Found'错误),并且代码运行。现在的问题是电子邮件没有发送。没有显示错误,记录被保存。 – wdarnellg