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

Laravel控制器更新数据库字段无报错但未生效求助

Troubleshooting Laravel Data Update Issue (No Errors, But Data Not Changing)

Hey there, I’ve run into this exact head-scratcher before—no error messages, redirect works like a charm, but the database and page data just won’t update. Let’s walk through the most likely fixes step by step:

1. You’re Missing the save() Call (Most Common Culprit!)

Looking at your code snippet, you’re assigning values to the model properties but never actually persisting those changes to the database. After setting your fields, you need to call save() to commit the updates:

public function update(Request $request, $id) { 
    $Myroutes = Myroutes::find($id); 
    $Myroutes->start = $request->input('start'); 
    // Add other field assignments here...
    
    // Critical line you’re probably missing:
    $Myroutes->save();

    return redirect()->back(); // Or your intended redirect path
}

Alternatively, use the cleaner update() method for bulk assignment (just requires a small setup step):

public function update(Request $request, $id) { 
    $Myroutes = Myroutes::find($id); 
    // Pass an array of fields you want to update
    $Myroutes->update($request->only(['start', 'end', 'your_other_fields']));

    return redirect()->back();
}

2. Model’s $fillable/$guarded Setup is Incorrect

Laravel protects against mass assignment vulnerabilities by default. If you use the bulk update() method (or even some manual assignment scenarios), you need to explicitly list which fields are allowed to be updated in your Myroutes model:

class Myroutes extends Model {
    // List all fields you want to allow updates for
    protected $fillable = ['start', 'end', 'description'];

    // OR, if you want to allow all fields (not recommended for security):
    // protected $guarded = [];
}

Without this, Laravel will silently ignore any fields not in $fillable—which explains why no errors pop up, but data doesn’t change.

3. Route/Controller Parameter Mismatch (Or Invalid ID)

Your route uses {myroute_id} but your controller accepts $id—while Laravel usually handles this fine, it’s worth verifying that the $id being passed actually corresponds to an existing record. Replace find($id) with findOrFail($id) to get a clear 404 error if the record doesn’t exist (this helps catch cases where you’re passing an invalid ID):

$Myroutes = Myroutes::findOrFail($id);

4. Frontend Form Isn’t Using the PUT Method

Laravel requires that PUT/PATCH requests are explicitly declared in HTML forms (since HTML forms only support GET/POST natively). Make sure your form includes the @method('PUT') directive:

<form action="/my-saved-routes/{{ $route->id }}" method="POST">
    @csrf
    @method('PUT') <!-- This tells Laravel to treat the POST as a PUT request -->
    <!-- Your form fields here -->
</form>

If you’re using AJAX, ensure your request uses the PUT method (not GET/POST).

5. Cache is Tricking You

Sometimes the issue is just stale cached data:

  • Clear your browser cache with a hard refresh (Ctrl+Shift+R)
  • Clear Laravel’s view cache: php artisan view:clear
  • Clear Laravel’s route cache: php artisan route:clear

6. Uncommitted Database Transactions

If you’re using database transactions in your code, double-check that you’re calling DB::commit() after making changes. If the transaction is rolled back (either explicitly or due to an uncaught exception), the data won’t be saved to the database.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:55:50