You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

编辑保存文章时出现Creating default object from empty value错误排查

Hey there, let's figure out what's causing that "Creating default object from empty value" error and fix it step by step!

First, the root cause of the error

The error on line 669 happens because when you run \App\News::find($id) (or \App\Event::find($id)), if there's no record matching that $id in the database, the $article variable becomes null. Then when you try to assign a value to $article->subject, PHP tries to create a default object from that empty null value—hence the error.

On top of that, there's a typo in your Event branch that will cause another error: you wrote $comm->comments = Request::input('comments'); but $comm is never defined. That should be $article->comments instead.

Here's how to fix this:

1. Handle missing records properly

Instead of using find($id), use findOrFail($id)—Laravel will automatically throw a 404 error if the record doesn't exist, which prevents the null object issue entirely.

Update your News branch:

$article = \App\News::findOrFail($id);

And your Event branch:

$article = \App\Event::findOrFail($id);

If you don't want a 404 and prefer to show a user-friendly message instead, you can manually check for the record:

$article = \App\News::find($id);
if (!$article) {
    return redirect()->back()->withErrors('Sorry, that news record doesn\'t exist.');
}

2. Fix the typo in the Event code

Change this line:

$comm->comments = Request::input('comments');

To:

$article->comments = Request::input('comments');

3. Optional: Simplify your code with mass assignment

You can clean up your controller code a lot using Laravel's mass assignment feature. First, add a $fillable property to your models to define which fields can be updated:

In App\News.php:

protected $fillable = ['subject', 'public', 'category_id', 'information'];

In App\Event.php:

protected $fillable = [
    'subject', 'comments', 'public', 'category_id', 'event_type_id',
    'country', 'starts', 'ends', 'organizer', 'address', 'city',
    'website', 'email', 'telephone', 'information'
];

Then you can replace all those individual property assignments with a single update() call:

For News:

$article->update(Request::only(['subject', 'public', 'category_id', 'information']));

For Event:

$article->update(Request::only([
    'subject', 'comments', 'public', 'category_id', 'event_type_id',
    'country', 'starts', 'ends', 'organizer', 'address', 'city',
    'website', 'email', 'telephone', 'information'
]));

This is cleaner and less prone to typos.

4. Double-check your form and route

Make sure your form is submitting the correct id value, and that the record actually exists in your database. You can add a quick debug line at the start of your update method to verify:

dd($id, Request::all()); // This will show you the ID and submitted data—remove after debugging

内容的提问来源于stack exchange,提问作者Andrei Nagy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:02:19