Argument 1 passed to App\Notifications\NewMessage::__construct() must be an instance of App\Models\User, null given
Я делаю уведомления на laravel и у меня возникает подобная ошибка.
Argument 1 passed to App\Notifications\NewMessage::__construct() must be an instance of App\Models\User, null given, called in C:\Users\pavel1988\Desktop\open_server_5_3_7_basic_full\OpenServer\domains\test\test\app\Http\Controllers\NotificationController.php on line 31
Вот мой контроллер:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use App\Models\Message;
use App\models\User;
use App\Notifications\NewMessage;
use Illuminate\Support\Facades\Notification;
class NotificationController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
// user 2 sends a message to user 1
$message = new Message;
$message->setAttribute('from', 2);
$message->setAttribute('to', 1);
$message->setAttribute('message', 'Demo message from user 2 to user 1.');
$message->save();
$fromUser = User::find(2);
$toUser = User::find(1);
// send notification using the "user" model, when the user receives new message
$toUser->notify(new NewMessage($fromUser));
// send notification using the "Notification" facade
Notification::send($toUser, new NewMessage($fromUser));
}
}
А это модель User:
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
Буду признателен любой помощи.