Skip to content

Call to undefined method Illuminate\Database\Eloquent\Relations\HasOneThrough::save() #62

@sts-ryan-holton

Description

@sts-ryan-holton

Hi, I'm trying to clone a model with relations that are MorphToMany but am getting an error on this. My Form model can be associated to many Site models in my project via a model called Siteable. My Siteable table looks like:

Schema::create('siteables', function (Blueprint $table) {
    $table->ulid('id')->primary()->index();
    $table->foreignUlid('site_id')->index()->constrained()->onUpdate('cascade')->onDelete('cascade');
    $table->ulidMorphs('siteable');
    $table->timestamps();

    // define indexes for performance
    $table->index(['site_id', 'siteable_type', 'siteable_id']);
    $table->index('created_at');
});

When trying to clone my Form, with the linked Siteable model, I get an error despite adding the following to my my Form model:

/**
 * Set clonable relations
 *
 * @var string
 */
protected $cloneable_relations = [
    'site'
];

For reference, these are the two relations in my Form:

/**
 * Get the site for the form
 */
public function site(): HasOneThrough
{
    return $this->hasOneThrough(
        Site::class,
        Siteable::class,
        'siteable_id',
        'id',
        'id',
        'site_id'
    )->where('siteable_type', Form::class);
}

/**
 * Get all of the sites for the form.
 */
public function sites(): MorphToMany
{
    return $this->morphToMany(Site::class, 'siteable')->withPivot('id');
}

The error is:

Call to undefined method Illuminate\Database\Eloquent\Relations\HasOneThrough::save()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions