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

Laravel异常:间接修改重载属性 | 应用版本存储API开发求助

Fixing the "Indirect modification of overloaded property" Error in Laravel

Hey there, let's break down this error you're hitting and get your saveNewVersion() method working smoothly. I've dealt with this exact issue a handful of times, so here's what's going on and how to fix it.

What's Causing This?

This error pops up when you try to directly modify a property that Laravel's Eloquent model serves up via magic methods (like __get). Eloquent uses property overloading to let you access model attributes easily, but PHP throws a fit if you try to modify those overloaded properties directly—since it doesn't handle references to them the same way as regular class properties.

Looking at your code snippet, the issue is almost certainly happening when you try to assign your built $app_dir value to a property on your Application model instance.

How to Fix It

Here are the key fixes tailored to your code:

1. Use setAttribute() Instead of Direct Assignment

Instead of doing something like this (which triggers the error):

$app->directory = $app_dir;

Use the model's built-in setAttribute() method, which safely handles setting attributes without hitting the overloading issue:

$app->setAttribute('directory', $app_dir);

2. Updated Full Code

Let's adjust your method with this fix, plus add some readability and edge-case handling:

public function saveNewVersion() {
    $artifact_url = Input::get('url');
    $artifact_id = Input::get('id');
    $artifact_name = Input::get('name');
    $artifact_version = Input::get('version');

    $urls = explode('/', $artifact_url); 
    $app_dir = "";
    // Clean up loop syntax for clarity
    for($i=5; $i<sizeof($urls); $i++) {
        $app_dir .= $urls[$i] . '/';
    }
    $app_dir .= $artifact_id;

    // Check if the artifact exists in the database
    $app = Application::where('artifact_id', $artifact_id)->first();

    if ($app) {
        // Safely set the directory attribute
        $app->setAttribute('directory', $app_dir);
        // Optional: Use fill() if updating multiple fields at once
        // $app->fill([
        //     'directory' => $app_dir,
        //     'version' => $artifact_version
        // ]);
        $app->save();
    } else {
        // Create a new record if the artifact doesn't exist
        $app = Application::create([
            'artifact_id' => $artifact_id,
            'name' => $artifact_name,
            'version' => $artifact_version,
            'directory' => $app_dir
            // Add any other required fields here
        ]);
    }

    // Add your remaining logic (like returning a response) here
}

3. Bonus: Verify Model Fillable Attributes

Make sure the fields you're setting (like directory, artifact_id, etc.) are listed in your Application model's $fillable array—this prevents mass assignment errors when using create() or fill():

class Application extends Model
{
    protected $fillable = ['artifact_id', 'name', 'version', 'directory'];
    // ... rest of your model code
}

Why This Works

Eloquent's setAttribute() method bypasses the property overloading system entirely. It directly updates the model's internal attributes array, which avoids the "indirect modification" error PHP throws when you mess with overloaded properties.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:31:36