1

I've got one error when trying to attach a program in Laravel with belongsToMany Relationship. Someone that knows what I'm doing wrong?

Controller

$user = User::where('id', $request->input('user_id'))->first();
$program = Program::where('id', $request->input('program_id'))->first();
$update = $user->programs()->attach($program);

Relationship

public function programs() : Relation
{
    return $this->belongsToMany(\App\Program::class, 'program_user');
}

I've got this error --> "Return value of App\User::programs() must be an instance of App\Relation, instance of Illuminate\Database\Eloquent\Relations\BelongsToMany returned"

nahoj
  • 285
  • 1
  • 4
  • 16

1 Answers1

2

Try to remove or change the : Relation to BelongsToMany from your programs() method. You are a saying that the function returns Relation, but it is returning a BelongsToMany relation.

Mateus Junges
  • 2,559
  • 1
  • 12
  • 24
  • You're my King! When I was removing the `: Relation` it started to work. Thank you so much! – nahoj May 25 '19 at 19:52