Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ufodisko's avatar

Error: Request failed with status code 500

I'm following: https://laracasts.com/series/lets-build-a-forum-with-laravel/episodes/43

So at the end of that episode, I realized that when I created User B to reply on a thread created by User A, I get the following error

Error: Request failed with status code 500

The reply gets saved into the database though and I can see it when I refresh the page, it just isn't rendering with vue and throwing an error.

Closer look at the network tab

message Too few arguments to function App\ThreadSubscription::notify(), 0 passed in C:\wamp64\www\forum\vendor\laravel\framework\src\Illuminate\Support\HigherOrderCollectionProxy.php on line 60 and exactly 1 expected

exception   Symfony\Component\Debug\Exception\FatalThrowableError

file    C:\wamp64\www\forum\app\ThreadSubscription.php

This is ThreadSubscription model

class ThreadSubscription extends Model
{
    protected $guarded = [];

    public function user()
    {
        $this->belongsTo(User::class);
    }

    public function thread()
    {
        return $this->belongsTo(Thread::class);
    }

    public function notify($reply)
    {
        $this->user->notify(new ThreadWasUpdated($this->thread, $reply));
    }
}

This is UserNotificationsController

class UserNotificationsController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        return auth()->user()->unreadNotifications;
    }

    public function destroy(User $user, $notifiationId)
    {
        auth()->user()->notifications()->findOrFail($notifiationId)->markAsRead();
    }
}

This is UserNotificationsFactory

$factory->define(DatabaseNotification::class, function (Faker $faker) {
    return [
        'id' => Uuid::uuid4()->toString(),
        'type' => 'App\Notifications\ThreadWasUpdated',
        'notifiable_id' => function () {
            return auth()->id() ?: factory('App\User')->create()->id;
        },
        'notifiable_type' => 'App\User',
        'data' => ['foo' => 'bar']
    ];
});

And this is addReply method in the User model

public function addReply($reply) {
        $reply = $this->replies()->create($reply);

        $this->subscriptions->filter(function($sub) use ($reply) {
            return $sub->user_id != $reply->user_id;
        })->each->notify();

        return $reply;

    }

EDIT: I forgot to pass $reply to each->notify() in the addReply method. But now I'm getting a different error.

message App\ThreadSubscription::user must return a relationship instance, but "null" was returned. Was the "return" keyword used?

exception   LogicException

file    C:\wamp64\www\forum\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php
0 likes
1 reply
mstrauss's avatar
mstrauss
Best Answer
Level 14

In your ThreadSubscription model, change the below

    public function user()
    {
        $this->belongsTo(User::class);
    }

to

    public function user()
    {
        return $this->belongsTo(User::class);
    }

You forgot the return statement on that relationship method.

Please or to participate in this conversation.