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

更新数据库触发BadMethodCallException:Method save does not exist

Troubleshooting "Method save does not exist" BadMethodCallException

Hey there, let's work through why you're hitting this error when trying to update your database record. This is a really common gotcha, so let's break down the most likely fixes:

1. You’re calling save() on a query builder instance (not an Eloquent model)

If your code looks something like this:

DB::table('your_table')->where('id', $recordId)->save();

That’s the problem! The DB::table() method returns a query builder instance, not an Eloquent model—and query builders don’t have a save() method.

Instead, you have two solid options:

  • Fetch the Eloquent model first, modify it, then save:
    $record = YourModel::find($recordId);
    $record->column_name = 'Updated value';
    $record->save();
    
  • Use the update() method directly on the query builder:
    DB::table('your_table')->where('id', $recordId)->update(['column_name' => 'Updated value']);
    

2. Your model doesn’t extend the base Eloquent Model

Double-check your model file (e.g., YourModel.php). It needs to extend Laravel’s base Eloquent Model class to inherit core methods like save(). Make sure it looks like this:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class YourModel extends Model
{
    // Your model logic here
}

If it’s missing the extends Model line, that’s exactly why the save() method isn’t available.

3. You’re working with a Collection instead of a single model

If you used get() to fetch records, you’re getting a Collection (a group of models), not a single instance. Collections don’t have a save() method built in.

For example, this will throw the error:

$records = YourModel::where('status', 'active')->get();
$records->save(); // ❌ Collection has no save() method

Instead, use first() to grab a single model instance:

$record = YourModel::where('status', 'active')->first();
if ($record) {
    $record->column_name = 'Updated value';
    $record->save(); // ✅ Works on a single model
}

4. You created a plain PHP object instead of an Eloquent model

If you accidentally instantiated a generic PHP object (like stdClass) instead of your model, it won’t have the save() method. Make sure you’re creating an instance of your Eloquent model:

// ❌ Wrong: plain object has no save() method
$record = new stdClass();

// ✅ Correct: instantiate your Eloquent model
$record = new YourModel();

Quick Debug Tip

If you’re unsure what type of object you’re dealing with, dump it out to inspect:

dd($record);

This will show you if it’s an Eloquent model, query builder, or collection—making it easy to spot the root issue.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 07:58:37