Home > Back-end >  Call to a member function sync() on null (many to many polymorphic laravel)
Call to a member function sync() on null (many to many polymorphic laravel)

Time:10-11

hi i have many to many polymorphic Relation

When I use the sync method to insert data in the interface table, I encounter this error. What is the problem?

controller code:

$job_create->skills()->sync($request['skill']);

job model:

   public function skills(){
    $this->morphMany(Skil::class,'skillables');
    }

skill model:

    public function jobs()
{
    return $this->morphedByMany(Job::class, 'skillables');
}

CodePudding user response:

Relationships should always return the relation.

public function skills(){
    return $this->morphMany(Skil::class,'skillables');
}

CodePudding user response:

first make sure that given Skil::class is correct i think it must be Skill::class

public function skills(){
 return $this->morphMany(Skill::class,'skillables');
}

the error says that $job_create->skills() is null you can test it with dd();

dd($job_create->skills()->get());

if the problem didn't solved just study the documentation: https://laravel.com/docs/9.x/eloquent-relationships#many-to-many-polymorphic-relations

  • Related