Open SanjinOsmanbasic opened 3 years ago
Spent a few hours yesterday trying to get to the bottom of this. Sharing in case it helps anyone.
My explanation below refers to @SanjinOsmanbasic's example.
There are two issues:
There is a simple answer though, which is to do all the necessary work in the QuestionResolver.
At first this doesn't seem possible as in the QuestionResolver you cannot access your 'answers'.
But it is possible, you just need the following in the set() method of the AnswerResolver:
public function set($resource, $attribute, $groups) {
$resource->answers = $groups
}
This means that in the QuestionResolver your answers will now be present in $groups:
$groups->each(function($group) {
$group->answers // here are your answers
});
This means you can create each question and then create the answers.
This is going to be a bit lengthy, so bear with me. But I also think that this could be later used as a concrete example of how to use Custom Resolver to save OneToMany relationships in the database - instead of JSON. And there is also a nested Flexible fields component that adds additional complexity that I didn't see covered in other issues
Ok, I'll try to explain:
Let's start with what I'm trying to do: I'm creating a Quiz admin panel where users can create Quiz, corresponding questions and answers to those questions. Flexible Nova content fields seemed like a perfect solution that i was having - Laravel nova relationships have too many clicks all around - so I wanted to use the Flexible fields to display these relationships in Quiz editing screen so user can enter everything right away - and the results will be saved in corresponding Questions and Answers tables which have there own models, etc.
App/Nova/Quiz.php
App/Nova/Flexible/Layouts/QuestionsLayout.php
App/Nova/Flexible/Resolvers/QuestionResolver.php
App/Nova/Flexible/Layouts/AnswerLayout.php
App/Nova/Flexible/Resolvers/AnswerResolver.php
I also needed to add SYNC method to OneToMany relationshop since it does not come as default in Laravel. I did it by addin g this macro ( that i found on StackOverflow) to
App/Providers/AppServiceProvider.php
Ok now that we have all the files ( hopefully i didnt forget something ) - its time for the tricky part.
So with the current state of code - I have the following issue.
Questions are saved to the table "questions" correctly, Answers also - but the issue is that in the AnswersResolver.php i dont have access to the original model of the Quiz, i just have access to the QuestionLayout. And that causes issues only when i create a new question ( In AnswerResolver ) because i cannot access Quizz ID which i can assign. This is also partially issue because the AnswerResolver is called before the QuestionResolver - so creating a Question in QuestionResolver is not an option. And in the QuestionResolver - if i wanted to populate the Answers - the issue is that i cannot use "find" method on the Collection of Layouts to find AnswerLayout since its located in the fields - and i was not able to access it and get the Layout for Answers to hidrate.
Does anyone have any idea how this could be solved, or at least push me in the right direction?
Im here for any further questions that you may have!