skip to Main Content

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');
}

2

Answers


  1. Relationships should always return the relation.

    public function skills(){
        return $this->morphMany(Skil::class,'skillables');
    }
    
    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search