wdarnellg
wdarnellg

Reputation: 67

Using Notification is Pivotal

I am trying to use the laravel 5.3 notification system. I have a many to many relationship on a couple of models. What I need to do is loop through all of the request data and send a notification to everyone appropriate. It seems that the notification methods won't work within a foreach loop. The error is:

BadMethodCallException in Builder.php line 2448: Call to undefined method Illuminate\Database\Query\Builder::routeNotificationFor()

The code I am trying to figure out is:

 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!']); 

}

Is there a way to collect related data and pass to notification methods?

UPDATE: The Players model looks like:

<?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);
  }
}

Upvotes: 0

Views: 154

Answers (1)

Eric Tucker
Eric Tucker

Reputation: 6345

Your $player model needs to use the Illuminate\Notifications\Notifiable trait.

Upvotes: 2

Related Questions