简体   繁体   English

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

[英]Error: Request failed with status code 500 in Laravel

I'm following: https://laracasts.com/series/lets-build-a-forum-with-laravel/episodes/43 我正在关注: 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 因此,在那集的结尾,我意识到当我创建用户B来答复用户A创建的线程时,会出现以下错误

Error: Request failed with status code 500 错误:请求失败,状态码为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. 不过,答复将保存到数据库中,刷新页面时可以看到它,只是它没有用vue渲染并抛出错误。

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

This is UserNotificationsController 这是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 这是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 这是User模型中的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;

    }

EDIT: I forgot to pass $reply to each->notify() in the addReply method. 编辑:我忘了将$ reply传递给addReply方法中的each-> notify()。 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

The error gives you what you need to do, in your ThreadSubscription model change your user method from 该错误为您提供了所需的操作,您可以在ThreadSubscription模型中将用户方法从

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

to this 对此

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM