I am trying to update or create a new record using the hasOneThrough relationship. A user can have many answers, and one answer can have one conditional answer. I can update the Answers relationship using the User, but I cant figure out how to update ConditionalAnswers through the Answers User relationship?
Error
Call to undefined method Illuminate\Database\Eloquent\Relations\HasMany::conditionalAnswers()
user
id
answers
id
user_id
conditional_answers
id
answers_id
User
public function answers(){
return $this->hasMany(Answers::class);
}
public function conditionalAnswers() {
return $this->hasManyThrough(ConditionalAnswers::class, Answers::class);
}
Answers
public function user(){
return $this->belongsTo(User::class);
}
public function conditionalAnswers() {
return $this->hasOne(ConditionalAnswers::class);
}
ConditionalAnswers
public function user() {
return $this->hasOneThrough(Answers::class, User::class);
}
public function answers() {
return $this->belongsTo(Answers::class);
}
Controller
$input = $request->all();
$user = User::find($input['user_id']);
$answers = $user->answers()->updateOrCreate(['user_id' => $user->id, 'key' => 'temp'], $input['answers']); // works fine
$conditionalAnswers = $user->answers()->conditionalAnswers()->updateOrCreate(['answers_id' => $answers->id, 'key' => 'temp_days'], ['value' => $input['conditional_answers']]); // doesnt work