I have a model Chapter which have field type than can be (article, video, quiz). To solve my problem I have to create a new model Quiz which will have relationship oneToOne with Chapter model whether type of Chapter == "quiz".
I've tried something like this and it works when type == "quiz" only. I guess I'll have to return other relations for other types for it to work, but I don't have nothing to return so I wanted to leave it empty for other types.
public function quizQuestions(){
if($this->chapter_type == "QUIZ"){
return $this->hasMany(QuizQuestion::class);
}
}
The point is, I want to somehow return empty relation for other types. I'm building an app in Laravel Nova and I wanted to hide Quiz table if a chapter is not type of "quiz" but it can't be done this way.
NovaDependencyContainer::make([
HasMany::make('QuizQuestions'),
])->dependsOn('chapter_type' , \App\Models\Chapter::QUIZ),
I might have missed something in documentation or I just don't understand something so thanks in advance for you help and advices!
CodePudding user response:
Try returning null otherwise:
public function quizQuestions(){
if($this->chapter_type == "QUIZ"){
return $this->hasMany(QuizQuestion::class);
}
return $this->hasMany(QuizQuestion::class)->where('id', -1);
}
CodePudding user response:
I advise you to re-think your Model, but not too many edits.
I have done things like you want in this manner:
return $this->hasMany('App\Models\Main\QuizQuestion','id','id')->where('chapter_type', '=', 1);
then ->where('chapter_type', '=', 2);
Works like a charm!!