0
votes

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"

1

1 Answers

1
votes

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.