Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

skoobi's avatar
Level 13

Indirect modification of overloaded property

Hi. Im having an issue saving to a relationship. I save to a model and then to the relationships which all work bar one. I copied it from all the others and still wont work. Its the Category one. The posts here seem to be related to JSON or arrays by the looks of it, but this is just an integer thats being saved, nothing different to the other relationships thats being used there currently.

I can't quite figure it out as its a only an integer im passing over. Here's the error: Indirect modification of overloaded property App\Models\Project::$hebci_category has no effect

MyController.php

$project = Project::create(
                [
                    'unique_id' => Str::uuid(),
                    'json_data' => [],
                    'step' => 1,
                    'locked' => 0,
                    'status' => $this->status,
                ]
            );

            $project->hebci_category->hebci_category_id = $this->category_id;
            $project->hebci_category->hebci_sub_category_id = $this->sub_category_id;
            $project->hebci_category->save();

			// Tried this as well to see.
			// $project->hebci_category->hebci_category_id = 1
            // $project->hebci_category->hebci_sub_category_id = 1;
            // $project->hebci_category->save();

            $project->details->title = $this->project_title;
            $project->details->description = $this->description;
            $project->details->start = $this->start_date;
            $project->details->end = $this->end_date;
            $project->details->save();

Project.php

public function hebci_category(): \Illuminate\Database\Eloquent\Relations\hasOne
    {
        return $this->hasOne(ProjectCategory::class, 'project_id', 'id');
    }

    public function details(): \Illuminate\Database\Eloquent\Relations\hasOne
    {
        return $this->hasOne(ProjectDetail::class, 'project_id', 'id');
    }

Any ideas? I've not come across this error before.

Many thanks

0 likes
3 replies
tykus's avatar
tykus
Best Answer
Level 104

Use the relation like this:

$project = Project::create([
    // ...
]);
$project->hebci_category()->create([
    'hebci_category_id' => $this->category_id,
    'hebci_sub_category_id' => $this->sub_category_id,
]);
skoobi's avatar
Level 13

@tykus Brilliant, thank you. I've not seen the create() used like that on a relationship before! That seems to have worked. Is there any reason why the other relationships work and that one didn't?

tykus's avatar

@skoobi the other relations should be handled in the same manner.

Please or to participate in this conversation.