错误:请求失败,Laravel中的状态码为500

时间:2019-06-30 09:16:33

标签: php laravel

我正在关注:https://laracasts.com/series/lets-build-a-forum-with-laravel/episodes/43

所以在那集的结尾,我意识到当我创建用户B来答复用户A创建的线程时,会出现以下错误

错误:请求失败,状态码为500

尽管回复已保存到数据库中,并且刷新页面后我可以看到它,只是它没有用vue渲染并抛出错误。

仔细查看“网络”标签

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

这是ThreadSubscription模型

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

这是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();
    }
}

这是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']
    ];
});

这是用户模型中的addReply方法

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;

    }

编辑:我忘记了将$ reply传递给addReply方法中的each-> notify()。但是现在我遇到了另一个错误。

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

1 个答案:

答案 0 :(得分:2)

该错误给出了您需要做的事情,在ThreadSubscription模型中,将用户方法从

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

对此

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